Javascript DOM How to - Get current html of a dom element








Question

We would like to know how to get current html of a dom element.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
function show(){<!--  w w w . jav a  2s .  c  o  m-->
   console.log(document.getElementById("abc").innerHTML);
}
window.onload=function(){
    document.getElementById("xyz").setAttribute('value','5');

    show();
}

</script>
</head>
<body>
  <div id=abc>
    <input id=xyz type=text value="2" />
  </div>
  <input type=button value="2" onclick="show()" />
</body>
</html>

The code above is rendered as follows: