0

I'm a bit confused with jQuery's .click() event. I'm trying to use the event on an image inside a span. This is the line of HTML containing the span and image.

<span class="content-message-element"><a href=""><img class="content-message-icon" src="./images/icon_close.png" alt="Close Message" /></a></span>

As you can see, the image has the class 'content-message-icon'. I've used this in my jQuery code (by the way - I have jQuery 1.7.1 included) but nothing happens ; the event is not triggered at all.

Is the .click() event limited to certain types of elements?

<script type="text/javascript">
        $(".content-message-icon").click(function() {
            alert("Handler for .click() called.");
        });
    </script>

This is my jQuery, any help is appreciated.

4
  • Try placing document.ready call before the click function. Commented Feb 17, 2012 at 5:14
  • i guess .click event not works with the class..u should use id only... Commented Feb 17, 2012 at 5:15
  • 4
    @CodeJack, that's simply not the case. the click event works as written. Commented Feb 17, 2012 at 5:16
  • ok ok sorry my bad...didnt know how i mentioned that.... Commented Feb 17, 2012 at 5:22

7 Answers 7

3

The click event should work fine. Try putting in the document ready code, like so:

$(function() {
    $(".content-message-icon").click(function() {
        alert("Handler for .click() called."); 
    });
});

Example with jsFiddle: http://jsfiddle.net/vGPfu/1/

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, works perfectly! I guess I have a little way to go with jQuery :P
Everyone has to start somewhere :). It's good to understand why code must be put in the document ready function, reading this page should give you a bit more info.
2

it may happen that u r firing click event before the DOM is ready. Use this ans

$(document).ready(function(){
        $('.content-message-icon').click(function(){
              //add click logic here
        });
});

1 Comment

@CodeJack click function has no restrictions it can be fired on anything i.e id,class,DOM element lyk anchor tag,div, span etc
1

you are not using

$(document).ready()

Comments

1

Other answers have mentioned using jQuery's document ready, but nobody (yet) has explained why, or what the other way to do it is. So: you can't reference an element from JavaScript (with or without jQuery) if the element has not been parsed yet. To assign an event handler (or do any other element manipulation from JS) the two ways to be sure the element has been parsed are:

  1. Put the script block after the element in the page source - anywhere after will work, but after all elements and just below the closing </body> tag is reasonably standard.

  2. Put the relevant code in a $(document).ready() handler (if using jQuery) or in an onload handler.

(The document ready handler is created at the point where that code is included, even before the elements it manipulates have been parsed, but it doesn't get executed until the whole document is ready.)

Comments

0

Try taking out the empty tag. The behavior of might be interfering here.

2 Comments

Are you referring to the <a> tag? That was there to make it seem clickable, I don't know if there's an alternate way to make it obvious my image can be clicked. I removed the tag and I still get the same behavior, though.
you can use css to make it "look" like a link.
0

This should work working..

Possible cause.. Jquery version = try other Browser Problem = try all browser

enclose into this jquery

$(document).ready(function()
{
  ...click event
});

Comments

0

Try this

<script type="text/javascript">
  $(".content-message-icon").live("click", function () {
    alert("Handler for .click() called.");
  });
 </script>

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.