2

Here's a snippet of my code:

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

And I have (hypothetically; in actuality 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?

2
  • Is your real code inside $(document).ready()? Commented Aug 15, 2009 at 23:59
  • Yes, it is – and I have other code working and executing, too. Commented Aug 16, 2009 at 19:59

7 Answers 7

5

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!");
});
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Your code snippet (<a href="#" class="item"><img src="1.jpg" /></a> )is being loaded dynamically? If so that's pretty crucial information!
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!
4

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

1 Comment

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!
0

Use bind.

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

2 Comments

if that ain't it, then there's something wrong somewhere else. docs.jquery.com/Events/bind#typedatafn
.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.
0

modifying the selector?

$(".item > img")

1 Comment

No dice. I don't think the problem is with the selector (I've tried many).
0

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.

Comments

0

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.

Comments

0

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.

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.