简体   繁体   中英

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!

尝试:

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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM