0

I am using JavaScript to assign a value to an empty div through document.getElementById('').innerHTML which works fine and I can see the words update and come up on the browser. I then try to capture those words but keep getting it as undefined.

My HTML is

<div id="name"> </div>

JavaScript executes this:

function doneloading(first_name) {
  document.getElementById('name').innerHTML = first_name;
}

I try to capture that using:

var photoname = document.getElementById('name').value;
alert(photoname);

Is it not possible to capture innerHTML in general?? Thanks in advance!

6
  • When are you calling that capturing code? Commented Mar 10, 2013 at 3:28
  • 1
    @Ahmad You are setting the innerHTML property, but retrieving the value property. You should try to retrieve the innerHTML property. Commented Mar 10, 2013 at 3:29
  • I am definitely calling it after the div has updated and shows the name on the browser Commented Mar 10, 2013 at 3:30
  • @kalaero Thanks for your comment. I tried that before posting my question and it didn't work either. Still came as undefined... Commented Mar 10, 2013 at 3:31
  • @kalaero I just tried it again and it worked this time. Wow. Thanks alot! Wouldn't have tried it again without your help :) Commented Mar 10, 2013 at 3:34

3 Answers 3

1

Try:

var photoname = document.getElementById('name').innerHTML;
Sign up to request clarification or add additional context in comments.

1 Comment

I tried that before and it still came up as undefined but its working now for some reason so thanks!
1

You can access the value within the element the same way you set it.

var innerHTML = document.getElementById('name').innerHTML;

This will also return any embedded HTML tags' code in the particular element. See w3school's documentation for the innerHTML property.

1 Comment

I tried that before and it still came up as undefined but its working now for some reason so thanks!
0

innerHTML output code is html code: Such as:

<div id="demo"><div id="name">The demo said is right</div></div>
var domNameHtml=document.getElementById('name').innerHTML;

the domNameHtml value is The demo said is right If you want change the dom object about div html code; Must be like this:

document.getElementById('name').innerHTML='<div id="name">The demo said is right</div><h1>Integral is my</h1>';

the html code Change :

<div id="demo"><div id="name">The demo said is right</div><h1>Integral is my</h1></div>

If you want change the dom object about div text code;

var text=document.getElementById('name').innerTest; the text value is [The demo said is right] Removed html code

Want to be able to help you

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.