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" "">

    <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') };


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

Post a Comment

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

Previous Post Next Post

Blog ads