Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting innerhtml value into the variable

Tags:

javascript

I am getting a value by using document.getElementById("forloop").innerHtml. And I am displaying that value in the file with some div id as

<div id="forloop"></div>.

But I want to assign that value to a variable in the file. Can you please how can I assign to the variable?

like image 503
sateesh Avatar asked Jun 27 '11 12:06

sateesh


People also ask

How do I get the innerHTML value?

Firstly, to get the innerHTML value of any tag, you either need that tag to have its 'id' property or 'name' property set. Then you can respectively use the 'document. getElementById(yourTagIdValue). innerHTML' or 'document.

Can you += innerHTML?

Appending to innerHTML is not supported: Usually, += is used for appending in JavaScript. But on appending to an Html tag using innerHTML, the whole tag is re-parsed.

How does innerHTML set value?

To set the value of innerHTML property, you use this syntax: element. innerHTML = newHTML; The setting will replace the existing content of an element with the new content.


2 Answers

var myInnerHtml = document.getElementById("forloop").innerHTML;

In most browsers, the property is named innerHTML (with HTML in all-caps), not innerHtml, so watch for that.

like image 82
FishBasketGordo Avatar answered Sep 19 '22 19:09

FishBasketGordo


If you want the value along with the HTML tags then you will use:

var x=document.getElementById("forloop").innerHTML;

Or if you want only the value then you will use:

var x=document.getElementById("forloop").innerText;
like image 25
samach Avatar answered Sep 22 '22 19:09

samach