5

I have an unordered list with some links. Some links have a class "A" others have a class "B". I'd like to call a jQuery function depending on which class is clicked, but I don't know how to do this.

5 Answers 5

8
$('.yourClass').click(function(e){
    //do something here
});
Sign up to request clarification or add additional context in comments.

7 Comments

Example: <li><b><a href="#" class="Page">test</a></b></li> then using $(document).ready(function(){ $('.Page').click(function(e){ alert('Clicked'); }); }); doesn't do anything, no errors if firebug but no alert.
take a look at this: jsfiddle.net/BdwTJ . The example you provided works. Maybe this is not your problem. Did you load query correctly?
jQuery is loaded correctly, it's being used for other things on the page without any problems. I made a new test. The list was being generated by the jquery tmpl plugin. If I create a list with identical properties by hand on the same page the click function works fine. I guess this is down to the rendering not working with my tmpl generated list.
try defining the click event after your list has been loaded
I've tried this also by having the click event at the in script tags below everything else, but before the closing html tag. Here is a proof of concept example of the problem stackoverflow.com/questions/5962556/…
|
4

Why don't you simply check the class inside the function ?

$("ol li a").click(function() {
    if ($(this).hasClass("A")) {
        // Do stuff if class = "A"
    } else if ($(this).hasClass("B")) {
        // Do stuff if class = "B"
    }
});

Comments

0
$(".A").click(function() {
  // do stuff for A
});

$(".B").click(function() {
  // do some other different stuff for B
});

Comments

0
jQuery('.A').click(function() { handler here })
jQuery('.B').click(function() { another handler here })

Comments

0
$("LI.A").click(function() {  
    //your stuff  
});

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.