0

Trying to execute a click event on a button:

$(".pReload", $(this).parent().parent().parent().parent()).trigger();

or

$(".pReload", $(this).parent().parent().parent().parent()).click();

However, it is not executing. The button is loaded dynamically. Is there any way I can use .live?

2
  • 2
    You shouldn't ever chain that many parent() calls together. Add an id or a class to whatever element you're trying to select, then use $('#myElementID') or $('.myElementClass') Commented Oct 26, 2011 at 2:21
  • 5
    @NRohler: Or use closest. Commented Oct 26, 2011 at 2:26

5 Answers 5

1

Created a .live click function for the dynamic button. The .click(); responded afterwards.

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

Comments

0

Try this:

$('.pReload').parent().parent().parent().parent().live('click', function(){
      // Your code
});

Hope it helps, all though it would be better if you assigned a class or id to the element youre trying to select with all the parent calls.

1 Comment

That binds a function to a click event to all future parent, parent, parent, parents of $('.pReload')... Don't think this is what the asker wanted.
0
$(".pReload", $(this).parent().parent().parent().parent()).trigger('click');

Comments

0

Are you loading the button in a $.ajax call? If so you can use a callback in the success method:

$.ajax({
 success: function({ $('.Preload')....});
});

Comments

0

You can use parents() method with eq() together, but the best solution is to have id for target element

var target = $('#target').parents('div').eq(-1);

See the work example here: http://jsfiddle.net/mikhailov/U8rXV/1/

1 Comment

The button/link that needs to be clicked is dynamically added to the page. If if was a .click function i would use .live but this is a triggered click.

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.