wrong values with JS innerHTML

I have this code:

<div id='html'><input type='text' value='' /></div><br><a onClick="alert(document.getElementById('html').innerHTML);">click me</a>

http://jsfiddle.net/EQ88q/

If you change input value and click on "click me" you won't see the real input value. Is there any propety instead of "innerHTML" that gets the real values?

PS: I'm using plain javascript I can't use any library.

Upvotes: 0

Views: 857

Answers (2)

Isaac
Isaac

Reputation: 11805

This will work, but it is quite crude:

<div id='html'>
<input type='text' value=''/>
</div>
<script>
function LinkClick()
{
 var ele = document.getElementById('html');
 ele.children[0].setAttribute("value",ele.children[0].value);
 alert(ele.innerHTML);
}
</script>
<br><a onClick="LinkClick()">click me</a>

I see ComCrude just provided this exact answer in the comments

Upvotes: 1

dzhg
dzhg

Reputation: 396

The elem.innerHTML is the HTML code inside of the element. In this case, the innerHTML is empty string as there is no content inside the tag.

You should use input_elem.value to obtain the value property of a input element.

Upvotes: 0

Related Questions