views:

269

answers:

7

Here's a snippet of my code:

    $(".item").click(function () {
        alert("clicked!");
    });

And I have (hypothetically; in actually it's far more complicated) the following on my page:

<a href="#" class="item"><img src="1.jpg" /></a>

However, when I click the image, I do not get an alert.

What is my mistake?

A: 

Use bind.

$(".item").bind("click", function(e) { ... });

Shaun
That doesn't work.
Isaac Hodes
if that ain't it, then there's something wrong somewhere else.http://docs.jquery.com/Events/bind#typedatafn
Shaun
`.bind('click', fn)` and `.click(fn)` are equivalent. In fact `.click()` uses `.bind('click', fn)`. Check out line 3099 of jQuery 1.3.2 un-compressed, un-minified source if you want to see for yourself.
Marve
+3  A: 

Is your selector actually matching anything? Try using the jQuery debug plugin (http://jquery.glyphix.com/) and doing this:

$(".item").debug().click(function() {
    alert("clicked!");
});

.debug() will log whatever is matched to the Firebug console (you are using firebug, right? :-) ) without "breaking the chain" so you can use it inline like this.

If that turns out correctly, there may be some issue with the browser navigating to "#" before it can show your alert. Try using the .preventDefault() method on the event object to prevent this behavior:

$(".item").click(function(evt) {
    evt.preventDefault();
    alert("clicked!");
});
Matt
This turned out not to work either... perhaps I should attach all of my code and the XHTML?Also, it seems as though (when I looked at the source) the dynamically added HTML (via JQuery's AJAX) isn't actually in the source code, but is viewable with Firebug or Safari's inspector. I'm not sure if that's normal or not.
Isaac Hodes
Your code snippet (<a href="#" class="item"><img src="1.jpg" /></a>)is being loaded dynamically? If so that's pretty crucial information!
Andy Gaskell
It is being added dynamically, and I honestly didn't realize that was significant. I am new to Javascript, really started using it just a couple days ago. Why does that change the way $.click() works? It seems counterintuative to me, but I suppose that .click() is bound to just the objects that are loaded in the DOM at the time the document is .ready(). So using live() or explicitly calling the click() function after the items have loaded is a fix. Thank you all!
Isaac Hodes
A: 

modifying the selector?

$(".item > img")

Andy Gaskell
No dice. I don't think the problem is with the selector (I've tried many).
Isaac Hodes
A: 

I had this problem recently after adding a context menu jquery plugin. The pluging was binding to the click event of the body and then unbinding click event - it seemed to remove all bindings to click event for all elements. Maybe a suggestion to turn off plugins or check you're not unbinding click for a parent element yourself.

max
+4  A: 

First question - are you adding the element to be clicked dynamically? If it is, you should use the live event since that will take care dynamically created elements.

http://docs.jquery.com/Events/live#typefn

Dhana
The fact that I was adding the events dynamically was the issues – I ended up fixing it by calling the function explicitly after the items were added in by calling the function from within the success function. Thank you!
Isaac Hodes
A: 

The code you have posted is correct, so I suspect there's something else going on that you haven't considered.

Firstly, if there was an error somewhere (even not in that exact bit of code) that might cause it to stop working. Put an alert just after this line to check that it runs.

Check that no other elements are catching the event and stopping it from propagating. This has bitten me before in the past... If there's anything else handling a click which has stopPropagation() or return false in it, that might be the problem.

nickf
A: 

One thing I've found (though only with links going elsewhere) is that adding return false; in may help, if it's just firing the anchor off instead of evaluating the alert. I can't really say why this would be the case, but that's a solution I found to a similar problem recently.

Dan G