Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Span.innerHTML returns text but Span.value returns undefined?

Tags:

javascript

dom

I have a function that populates a pages with something like this

<span id="span_title_'+result_no+'">'+title+'</span>

and then I have another function that has this:

      document.getElementById("span_title_"+which_table).innerHTML="asg";
alert(document.getElementById("span_title_"+which_table).value);

The strange thing is the first (innerHTML) call works perfectly, the second one, the alert, gives me "undefined"

Any idea why this is?

like image 571
Ryan Avatar asked Jun 20 '11 15:06

Ryan


People also ask

Can we use innerHTML in span?

The innerHTML property returns: This element has extra spacing and contains <span>a span element</span>. The textContent property returns: This element has extra spacing and contains a span element.

Why is innerHTML undefined?

InnerHTML returning undefined for <code> tag in HTML Of that collection, your code as it is now is not specifying which member of that collection it wants to use. In that case there is a difference in that it returns a live list. But you would still want to specify which you wanted.

Why innerHTML does not work?

People can struggle and complain about innerHTML not working. Such things usually occur because of human error, when strings are not appropriately defined, or there are some mistakes in JavaScript code.

What does .innerHTML return?

The innerHTML property returns the current HTML source of the element, including any change that has been made since the page was loaded. Do not use innerHTML to set new contents that you have no control to avoid a security risk.


2 Answers

<span> DOM elements don't have a value property. Use innerHTML to read the contents.

like image 81
Ates Goral Avatar answered Oct 18 '22 22:10

Ates Goral


span doesn't have attribute with the name "value" only innnerHTML, you should use innerHTML for second call.

like image 27
Senad Meškin Avatar answered Oct 18 '22 21:10

Senad Meškin