5attab
5attab

Reputation: 29

Capture innerHTML using document.getElementById

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!

Upvotes: 0

Views: 3803

Answers (3)

demo
demo

Reputation: 111

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

Upvotes: 0

Aiias
Aiias

Reputation: 4748

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.

Upvotes: 1

hunterloftis
hunterloftis

Reputation: 13799

Try:

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

Upvotes: 1

Related Questions