Preetam Purbia
Preetam Purbia

Reputation: 5942

How to get current html of a dom element

I am trying to retrive html of a dom element "abc".

<div id=abc>
<input id=xyz type=text value="2" />
</div>
<input  type=button value="2" onclick="show()"/>
<script>
document.getElementById("xyz").value=5;
function show(){
alert(document.getElementById("abc").innerHTML);
}
</script> 

Lets say I modified the value of text box

  1. by entering some value OR
  2. by Javascript code

    document.getElementById("xyz").value=5;

but

alert(document.getElementById("abc").innerHTML);

always show me

 <input id=xyz type=text value="2" />

Why? Can anyone tell me how I can get the latest html of the dom element "abc".?

Upvotes: 1

Views: 2847

Answers (1)

mako-taco
mako-taco

Reputation: 732

.value will not change the HTML. If you want to do that, use .setAttrubute instead. See this JSFiddle: http://jsfiddle.net/5EFXN/

document.getElementById("xyz").setAttribute('value','5');

Upvotes: 2

Related Questions