2

Is there a way to make a $(selector).click(function) run before, or preceed, an inline onClick="function"?

Also, if possible, I would like to stop the inline function from running entirely (by using preventDefault() inside the $(selector).click()).

Here's a fiddle to illustrate the precedence: http://jsfiddle.net/yUZWR/1/

1 Answer 1

1

Assuming you meant the anchor ID to be called link and not #link, this is how to reverse the function execution order...

var v = $('#link').attr('onclick');

$('#link').removeAttr('onclick').click(function(){
    alert('captured');
    return false;
}).click(function() { eval(v); })

I'm certain there's a better way of doing this i.e., not using eval. To prevent the inline function running altogether, just omit the second call to click.

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

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.