How to call the onclick event in dynamic div tag

by Nirmala 2009-06-05 16:49:25

Hi...
Use the below code to create div tag dynamically and call the javascript function in onclick event.

<html>
<head>
<style>
.dynamicDiv
{
background-image:url(img1.jpg);height:200;width:320; //set the image path correctly
}
</style>
<script type="text/javascript">
function create_element()
{
var divTag=document.createElement("div");
divTag.id="div0";
divTag.setAttribute("class","dynamicDiv");
divTag.onclick=function(){test();};
document.body.appendChild(divTag);
}
function test()
{
alert("call the javascript function");
}

</script>
</head>
<body onload="create_element()">
</body>
</html>

Tagged in:

9058
like
2
dislike
0
mail
flag

You must LOGIN to add comments