Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

Good day!

I am creating an insert row to an HTML table that includes an INPUT TYPE="button":

JavaScript
var table = document.getElementById('tblSkill');           
var row = table.insertRow(-1);

var rowCount = table.rows.length - 1;

var cell0 = row.insertCell(0);
var cell1 = row.insertCell(1);
var cell2 = row.insertCell(2);

var skill = document.getElementById('txtSkill').value;
var values = document.getElementById('txtValue').value;            

cell0.innerHTML = skill;
cell1.innerHTML = values;
cell2.innerHTML = "<input type='button' value='edit' size=" + lastId + " title=" + rowCount + " name='btnEdit' class='btnEdits'  />"   




The button edit(class='btnEdits') has a jquery function when clicked:
JavaScript
$(".btnEdits").click(function(){                

var v_param = 0;
var v_value = 1;
var v_id = 2;

var r = this.title;                  
var table = document.getElementById('tblSkill');                       
$("#div_edit").show();   

document.getElementById('txtSkill').value = table.rows[r].cells[v_param].innerHTML;
document.getElementById('txtValue').value = table.rows[r].cells[v_value].innerHTML;
document.getElementById('hiddenID').value = table.rows[r].cells[v_id].innerHTML;
document.getElementById('hiddenRow').value = r;
document.getElementById('hiddenAdd').value = '0';           

});



My problem here is that the jQuery function ($(".btnEdits").click) is not firing when i add the html button DYNAMICALLY.

Please kindly give me an advice. Thank you in advance :)
Posted

1 solution

That's because events attached to any html elements do not apply to elements created dynamically. Use jQuery's "live" method to declare the click event for elements created dynamically. Refer to "live" here - http://api.jquery.com/live/[^]

In effect you would have to do something like this:

JavaScript
$('.btnEdits').live('click', function () {
    alert("button clicked");
});


Hope this helps!
 
Share this answer
 
Comments
akosidab 16-May-12 22:32pm    
This is what I was looking for! :D :D :D

Two thumbs up for you Karthik!
Karthik. A 16-May-12 22:33pm    
Thanks :) You are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900