How to call the onclick event in dynamic div tag - Javascript Views : 2895
Tagged in : Javascript
2 0
Send mail
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>
By Nirmala, On - 2009-06-05



    Login to add Comments .