1

I am generating some buttons dynamically with dynamic ids. I wish to handle each button's click based on its id. But that's not working.

Following is my code:

var del_id = "del"+countPage+"-"+questionCount;
 html+='<tr><td><input type="button" value="delete" id="'+del_id+'" /><td><input type="button" value="Copy" id="copy_id"/></tr>';
        $("#"+del_id).click(function(){
                                alert("jkhkjh");
                              });

If I give static id instead of del_id, then it works. Where am I getting wrong? How do I solve this?

edited:solved

html+='<tr><td id="'+del_id+'"><input type="button" value="delete" id="'+del_id+'" /><td><input type="button" value="Copy" id="copy_id"/></tr>';


        $(document).on("click", "#"+del_id, function(){
                       alert("jkhkjh");
                       });

6 Answers 6

2

You have to use Event-delegation to hook the events with elements which are loaded dynamically.

Try,

  $(document).on("click", "#"+del_id, function(){

Or try like this, dont register events for each and every buttons since they are performing the same task.

  $(document).on("click", "[type='button'][id^='del']", function(){

Or add a common class to that button element and try like this,

  $(document).on("click", ".buttonClass", function(){
Sign up to request clarification or add additional context in comments.

5 Comments

this works but I get the alert 2 times. Any idea? I just want the alert one time
@z22 which method you had adopted for your code..?\
$.on(click)- the one you suggested
i have included the solved code in my question edit- please review
@z22 Please note that Id must be unique.. You have assigned saeme id for two elements in your code.. Just change that as a class.
1

Try this for dynamically created buttons

$(document).on('click', 'buttonId/Class', function(){
   // Do something.
})

2 Comments

this works but I get the alert 2 times. Any idea? I just want the alert one time
you are using same id on two element, on td and input too. Id should be unique on the page.
1

Use

$(document).on("click", "#"+del_id, function(){

});

2 Comments

this works but I get the alert 2 times. Any idea? I just want the alert one time
your td id and input id is same so only you get 2 alerts change this
0

You are giving the same id to 2 different DOM elements (TD and input). Id's are supposed to be unique.

jQuery then gets only the first DOM element that it finds.

Comments

0

**.live()** - This method provides a means to attach delegated event handlers to the document element of a page, which simplifies the use of event handlers when content is dynamically added to a page.

$( "#dymanic" ).live( "click", function() { alert( "Goodbye!" ); // jQuery 1.3+ });

Refer to this documentation

https://api.jquery.com/live/

Comments

0

if your controls are being added dynamically on page, they should be handled with special events that take future events into account. like on() or delegate().

I will prefer delegate() as I used it numerous times.

$('body').on("#"+del_id, 'click', function(){
   //Do something.
});

Note: with delegate() or on() you have an advantage of attaching multiple events handlers and make your selector search faster as you can specify the parent element to search within. In this case I mentioned body as parent selector. you can specify a more closer parent node.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.