0

I made a jquery code like :

$('button').click(function(event) {

});

In it; I put $.post and sent data to a php file and return table rows. In every rows, there is an 'add' button.

Then I made another jquery code for these buttons like :

$('.row_button').click(function(event) {

});  

Again, I put $.post and tried to send data about that row and wanted to fetch information via ajax request. But it didn't work. Nothing happend. I looked code and there is no error.

Isn't it possible to use ajax within another ajax? Or is there another way? Thank you.

2
  • 1
    There is no such thing as "ajax within another ajax". When you're in the success callback of the first .ajax call, you're not in the first .ajax any more. The "a" in "ajax" stands for "asynchronous". Commented Jul 27, 2012 at 14:02
  • maybe if we look how it's your html markup and the entirely function we can help you Commented Jul 27, 2012 at 14:02

2 Answers 2

7

That is because those newly injected elements don't know about the click event binding you already have.

Solution : use jquery on function.

Change

$('.row_button').click(function(event) {

});

to

$(document).on("click",".row_button'",function(event) {

});

jQuery on works for current and future elements (newly injected elements via ajax/dynamically adding new elements using javascript) . It is available from jQuery 1.7+ version. If you are using an earlier version of jQuery, consider using jquery live (As of jQuery 1.7, the .live() method is deprecated)

Sign up to request clarification or add additional context in comments.

1 Comment

Older versions should use .delegate(), not .live(). I'd update your answer to suggest using that instead.
3

Try to use this instead second code:

$('.row_button').live('click',function(event) {
  ...
});  

Jquery .live attach an event handler for all elements which match the current selector, now and in the future.


Edit

.live is now deprected so use .on insted:

$(document).on("click",".row_button'",function(event) {

});

From Jquery documentation:

$(selector).live(events, data, handler);                // jQuery 1.3+
$(document).delegate(selector, events, data, handler);  // jQuery 1.4.3+
$(document).on(events, selector, data, handler);        // jQuery 1.7+

6 Comments

The live functionality here does not delegate the event to a static container. The 'row-button' is propagated within the ajax call. What do you think using 'live' here is achieving?
@Ohgodwhy .live() does delegate events, which is what they want. It's the same as doing $(document).on('click', '.row_button', function() {...}); or $(document).delegate('.row_button', 'click', function() {...}):, but also happens to be deprecated.
@GeenHenk Yup, +1 for delegate since live is deprecated.
@Anthony Grist the point I was making that using live in that scenario is doing nothing. You're not using live to delegate the click handler to a static container. Without doing such, it's effectively useless. SOrry you were confused.
|

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.