15

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?

5 Answers 5

18
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.

Sign up to request clarification or add additional context in comments.

2 Comments

all browsers, not most browsers
@mplungjan I always leave a little wiggle room where I can. :)
11

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;

1 Comment

innerText is not supported by all browsers that support innerHTML - sniff for textContent and give innerText if not there
3

I'm not sure if i understand right, maybe this?

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

Comments

0

Don't know where the strech is:

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

Comments

0

Is the innerHTML of <div id="forloop"></div> added through JavaScript? If so, you might want to make sure that the var myInnerHtml = document.getElementById("forloop").innerHTML gets executed after the innerHTML has been added.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.