Populate Div with JavaScript

We can fill div dynamically with the help of JavaScript, using innerHTML.

As the example given below.

<html>
<head>
<script type=”text/javascript”>
function fillDiv(){
var sub = document.getElementById(‘msgDiv’);
var txtVal = document.getElementById(‘txt’).value;
sub.innerHTML = “Entered Text is “+txtVal;
}
</script>
</head>
<body>
Enter Text : <input type=’text’ id=”txt”/>
<input type=”button” onclick=”fillDiv()” value=”Show”/>
<div id=”msgDiv”>

</div>
</body>
</html>

Leave a Reply