3

As I mentioned in the title, I would like to prevent an event from being triggered when I fire another one:

Let's take this example:

 $(function() { $("#security_code").bind('change', function () { // Do validation and show errors }); $("#reloadCaptcha").bind('click', function () { // generate new captcha and clear 'secuirity code' field // clear the field so the user will enter the new generated security code document.getElementById('security_code').value = ''; }); }); 

PURPOSE: prevent the 'change' event from being fired when the '#reloadCaptcha' is clicked.

When I click the link (second event) the first event fires too (since 'change' activates). I am looking to stop all "change" events when a link is actually clicked. I hope I was clear enough. Does anyone have any ideas?

And yes, I've tried unbind, stopPropagation and other functions from the jQuery documentation, but it just didn't work.

Note that I only want to have the "change" freezed when I click that specific link. In other cases, it should work all the time.

Thanks in advance guys! I hope there is someone out there that can clarify this for me.

2
  • Is the code inside of the click handler for the #link_id element changing the value of the #username field, and thats why the change event fires? Just looking for clarification Commented Mar 20, 2011 at 22:33
  • Yes, the code is actually inside the click handler for #link_id: document.getElementById('security_code').value = ''; Commented Mar 20, 2011 at 22:43

6 Answers 6

1
$(function() { $("#security_code").bind('change', function () { // If #security_code is empty do nothing if ($('#security_code').val() == '') { return; } // Otherwise validate... }); $("#reloadCaptcha").bind('click', function () { // generate new captcha and clear 'secuirity code' field // clear the field so the user will enter the new generated security code $('#security_code').val(''); }); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

It just doesn't work! it looks like the 'change' is fired before the 'click'! Thanks for all your replies guys. I can't believe this apparently tiny thing is causing me so much trouble.
this may be simple but have you tried moving your click event above your change event handling in code?
0

Unless the code that fires when $("#link_id") is clicked is changing the element $("#username"), the change event shouldn't fire... But either way event.preventDefault() should do what you want.

3 Comments

Actually, it does change it. I use a refresh image link that clears a security code field. The problem is: when I click refresh a new captcha is generated and the security code field is cleared so the user can enter a new code. But when it is cleared, the change event fires and it says that the code is incorrect and it looks unprofessional.
i would have thought preventDefault() will only apply to the element within the bind() statement whereas in this case he is working across two separate elements?
Here's a new version of the code:<script type="text/javascript"> <!-- $(function() { $("#security_code").bind('change', function () { // Do validation and show errors }); $("#reloadCaptcha").bind('click', function () { // generate new captcha and clear 'secuirity code' field document.getElementById('security_code').value = ''; }); }); //--> </script>
0

From jQuery documentation:

To stop further handlers from executing after one bound using .live(), the handler must return false. Calling .stopPropagation() will not accomplish this.

Have you tried using .live() instead of .bind() and returning false?

Comments

0

It's hard to answer without seeing the whole code but maybe the following code would help. I know it's not a perfect solution but might help;

$("#username").bind('change', function () { if ( $('body').data('linkClicked') !== 1 ) { // Validate field when element loses focus } }); $("#link_id").bind('click', function () { $('body').data('linkClicked', 1); // Do something when the a specific link is clicked }); 

1 Comment

Many thanks for your reply! Just posted a new version of the code so you can understand better what I want.
0

Here's a new version of my code:

$(function() { $("#security_code").bind('change', function () { // Do validation and show errors }); $("#reloadCaptcha").bind('click', function () { // generate new captcha and clear 'secuirity code' field // clear the field so the user will enter the new generated security code document.getElementById('security_code').value = ''; }); }); 

PURPOSE: prevent the 'change' event from being fired when the '#reloadCaptcha' is clicked.

2 Comments

If in your click event you're resetting the value to nothing, in your change event can't you just tell it to skip validation if value is nothing? See my 2nd answer...
Correct, but it looks like the 'change' fires before the 'click' and it doesn't work!
0
$(function() { $("#security_code").bind('change',function() { if ($(this).val() == 'something') { $(this).bind('change'); //do something else... } }); $("#reloadCaptcha").bind('click',function() { $("#security_code").val('').unbind('change'); }); }); 

PS: the one from timothyclifford is also good.

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.