0

I have a table with and id of foo and I want to add rows dynamically that have a button that do something. How can I assign the listeners for the buttons dynamically using JQuery?

Thanks

2 Answers 2

2

You can use the bind method to assign event listeners if your buttons are already present or use the live or delegate methods if the buttons are generated dynamically.

Update:

Try with live() method:

$('.edit').live('click', function(){
  // your code here...
});
Sign up to request clarification or add additional context in comments.

2 Comments

I tried using bind. I gave my button a class of edit and I selected based on $('.edit'). Am I doing something wrong if it's in a table?
Ok it works but rather than run the function once it runs it more than once. Is that supposed to happen? Is there a way I can just make the function run once?
0

you can assign class to dynamically added buttons and write something like if class is adder

$(function(){
   $(".adder").live("click",function(){
        //do something
});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.