Cummings.P Cummings.P - 2 years ago 68
jQuery Question

Jquery not recognizing a clicked button

I am having trouble with jquery with regards to the

method. I have the following code:

text += "<input type='button' id='create_setup_button' value='Add SetUp' />";
text += "<input type='button' id='create_teardown_button' value='Add Teardown' />";
text += "<input type='button' id='create_event_button' value='Create Event' />";

$("#create_setup_button").click(function () {
alert("what is good?");

Is there something I am missing that is causing the button not to click? Cheers ~

Answer Source

this is clearly not all your code. What happens to the text variable? when does it go in the dom?

If your click handler comes before putting text in the dom, then it will not work because you click handler can not find the element at time of execution.

If you use:

$('body').on('click', '#create_setup_button', function () {
    alert("what is good?");

it should work because you have an event delegate on the body that will trap all bubbling click events on elements that match the #create_setup_button selector.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download