I have added image & button dynamically to a webpage. I wrote a "onclick" event, by which the image will be changed on clicking respective button. But the code doesn't work.
My code is;
<style>
.tableclass{
height:80px;}
.imgclass{
height:50px;
width:50px;} </style>
<script>
var img = new Image();
img.src = "Images/circle.png";
img.className = 'imgclass';
window.onload = function init() {
for(var i=0;i<5;i++)
{
var container = document.getElementById('body');
var table = document.createElement('table');
table.className = 'tableclass';
var tr = document.createElement('tr');
var td = document.createElement('td');
var btn = document.createElement('input');
btn.type = 'button';
btn.className = 'buttonclass';
td.appendChild(btn);
td.appendChild(img);
tr.appendChild(td);
table.appendChild(tr);
container.appendChild(table);
}
}
btn.onclick = function(){
document.getElementsByClassName('imgclass').src = "Images/square.jpeg";
};
</script>
<body id="body"></body>
<body>tag