2

Code is not running on .click when I have this:

$(".cancel").click(function() {
    alert("got here");
    $(this).closest(":dialog").dialog("close");                            
});

<a class="cancel" href=""><img src="images/cancelButton.gif" border="0" /></a>

It's got to be something stupid, but I cannot see it.

5 Answers 5

7

I would assume you're wrapping the code in $(document).ready( function () { ... }); ?

$(document).ready( function () {
  $('.cancel').click( function(e) {
     alert('here!');
     e.preventDefault(); // prevents click from following through
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Important to note the "why" here: without the "document ready" condition, the target link does not yet exist in the DOM and cannot be modified by jQuery.
Yes, good point. I think people forget that javascript is run as soon as it's read, the ready() command essentially attaches its contents to a documentIsNowFinishedLoadingIntoBrowserMemory event. :-)
1

add return false at the end of your click event:

$(".cancel").click(function() {
    alert("got here");
    $(this).closest(":dialog").dialog("close");
    return false;                            
});

5 Comments

its pretty unclean to use return false! Do this $(".cancel").click(function(o) { alert("got here"); $(this).closest(":dialog").dialog("close"); o.preventDefault(); }); an do it on $(document).ready(function(){ then it should work })
why is that? I added that but the dialog did not close.
why is it unclean? it does the same thing as preventDefault and in less code.
for the simple reason that the function is returning false. if you want to make a click event listener later in your project and want your click function to return something. its gonna be problematic if they all return false...
what can i click handler function return other then true or false? i dont think i follow you.
0

If href="" instead of href="#" doesn't that mean it actually reloads the page on click?

1 Comment

ah, well if I don't put a href in there I get no clickability
0

Anywhere you put an onclick event with a javascript call, if you add return false then the default behavior (what would have happened without the onclick event) will be disregarded.

And so in this case return return false will prevent the browser jump to the link anchor.

Edit

Fair comment below - looked at the jquery.com and it says

Returning false from a handler is equivalent to calling both .preventDefault() and .stopPropagation() on the event object.

2 Comments

its not. because your function is always gonna return false. If you are using a event listener for example a false return it can be problematic.
yes, I get what you mean now, from your comment on the answer above 'its gonna be problematic if they all return false'
0

A couple of people have said add return false; to your click handler. You should instead use the preventDefault method on the event class like so:

$(".cancel").click(function(event) {
  event.preventDefault();
  alert("got here");
  $(this).closest(":dialog").dialog("close");
});

Make sure you pass in the event to the handler function. This is the preferred jQuery way. I also always put the preventDefault() as the first thing in the handler method, but I don't think that makes a difference.

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.