How To attach event to runtime created control

In this post, I will show how to create a control at run time and attached event to control.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Untitled</title>
</head>
<body>

    <script type="text/javascript">
        function addRow() {
            var table = document.getElementById('table1');
            var TR = table1.insertRow();
            var TD = TR.insertCell();
            TD.innerHTML = 'Row added';
            TD.onclick = function() { alert('This is onclick') };
            TD.onmouseover = function() { alert('This is mouseover') };

        }
    </script>

    <table id='table1'>
    </table>
    <br>
    <input type='button' value='Add Row' onclick='addRow()' />
</body>
</html>

Please do not post any spam link in the comment box😊

إرسال تعليق (0)
أحدث أقدم