tags:

views:

56

answers:

5

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.

A: 

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

Jeriko
ah, well if I don't put a href in there I get no clickability
CoffeeAddict
+6  A: 

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
  });
});
Atømix
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.
Adam Backstrom
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. :-)
Atømix
+1  A: 

add return false at the end of your click event:

$(".cancel").click(function() {
    alert("got here");
    $(this).closest(":dialog").dialog("close");
    return false;                            
});
mkoryak
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 })
meo
why is that? I added that but the dialog did not close.
CoffeeAddict
why is it unclean? it does the same thing as preventDefault and in less code.
mkoryak
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...
meo
what can i click handler function return other then true or false? i dont think i follow you.
mkoryak
A: 

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.

Nicholas Murray
use preventDefault() not return false !!!
meo
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.
meo
yes, I get what you mean now, from your comment on the answer above 'its gonna be problematic if they all return false'
Nicholas Murray
A: 

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.

Hooray Im Helping