4

I would like to bind the same event to a list of anchor link. Why this does not work?

Markup:

<a tabindex="0" href="#contactRoles" 
    class="fg-button fg-button-icon-right ui-widget ui-state-default ui-corner-all" 
    id="contactAdd">
    <span class="ui-icon ui-icon-triangle-1-s"></span>Aggiungi contatto</a>

<div id="contactRoles" class="hidden">
<ul>
    <li><a href="#1" class="contactRole">Cliente</a></li>
    <li><a href="#2" class="contactRole">Controparte</a></li>
    <li><a href="#3" class="contactRole">Avvocato</a></li>
    <li><a href="#4" class="contactRole">Avv. Controparte</a></li>
    <li><a href="#5" class="contactRole">Altre parti</a></li>
    <li><a href="#6" class="contactRole">Domiciliatario</a></li>
    <li><a href="#7" class="contactRole">Pubblico Ministero</a></li>
    <li><a href="#8" class="contactRole">Giudice</a></li>
    <li><a href="#9" class="contactRole">Istruttori</a></li>
    <li><a href="#10" class="contactRole">Studio Legale</a></li>
</ul>
</div>

jQuery:

$('#contactAdd').menu({
    content: $('#contactRoles').html(),
    width: 150,
    showSpeed: 300
});

$("a.contactRole").click(function(event){
    event.preventDefault();
    alert("Link " + $(this).attr("href") + " clicked");
});

Where am I wrong?

EDIT: @everybody: Yes the script is wrapped by a $(document).ready(...)

For further information consider that the div with class "hidden" is hidden and can only be viewed by a click on another anchor as you can see from this screenshot. alt text

12
  • Lorenzo - Should still work. Are the a.contactRole elements added to the page dynamically (sometime after the page loads)? Commented Sep 10, 2010 at 0:18
  • No Patrick. They are only showed when clicked but that's static markup on the page... Commented Sep 10, 2010 at 0:25
  • Lorenzo - Try the edit I did at the top of my answer. Your code should work, but who knows. Also, does other jQuery code work on the page? Commented Sep 10, 2010 at 0:27
  • @Patrick: I have done another edit to the question to add the complete markup and jQuery code for the Button that shows the menu. Now I try with your suggestion... Commented Sep 10, 2010 at 0:32
  • It does not work even with the "live" approach! :( Commented Sep 10, 2010 at 0:40

3 Answers 3

4

EDIT: Based on your comment, you're already wrapped with a ready() function.

Another possibility is that the a.contactRole elements are added to the DOM after the page loads.

If that's the case, try this:

$(function() {
    $("a.contactRole").live('click', function(event){
        event.preventDefault();
        alert("Link " + $(this).attr("href") + " clicked");
    });
});

Original answer:

Should work. Have you made sure the document is loaded before assigning the click handler?

If the <a> elements haven't loaded when you try to assign the handler, it won't work.

Example: http://jsfiddle.net/kjSG8/

   // Wrap your code with $(function() {...}) to make sure it doesn't
   //    run until the DOM is fully loaded
$(function() {
    $("a.contactRole").click(function(event){
        event.preventDefault();
        alert("Link " + $(this).attr("href") + " clicked");
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

It seems like a standard event, are you declaring it within a $(document).ready(function() { }); block?

Comments

0

Ensure you create the click when the DOM is ready. You could also just say $(".contactRole").click instead of $("a.contactRole").click

2 Comments

To your second statement...I could be wrong, but I believe the jQuery selection is much faster when you define the element ahead of the classes.
@JasCav - I would agree with you there, but I find when people are new to jquery selectors, it's best to simplify the problems until a firm grasp is understood. (+1 Anyway) ^_^

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.