0

So I have this PHP code

    </div>
    <div id="topmusicartisttitle">Top 5 Music Artist</div>
    <div class="edit"><a href="" name="topartistsedit">edit</a></div>
    <div id="topmusicartistlist">
    <ul>
    ...

that basically passes a list of stuff and I want to be able to click on this a href in javascript but I don't want it to go anywhere I just want to catch the click and handle it. So to start I have:

   $('a[name=birthdayedit').live('click',function(e){
  e.preventDefault();
  });

But this doesn't seem to work. I checked firebug and the href and the name are there (obviously), but the click isn't registered in Javascript and it still redirects. I assume live is the function to use since this is pretty much dynamically created content. Any one know what I'm doing wrong?

5
  • Have you tried including return false; in your click handler? Commented Oct 4, 2011 at 22:03
  • 1
    There's no birthdayedit named field in your snippet, and you're missing the closing ] in the jquery selector. Commented Oct 4, 2011 at 22:04
  • 4
    $('a[name=birthdayedit') is an error. Compare with $('a[name=birthdayedit]'). Commented Oct 4, 2011 at 22:04
  • If you place a window.alert("CLick"); in the click handler do you get an alert? You might try adding a "]" to close out the "a[name=]" part. Commented Oct 4, 2011 at 22:04
  • Your're missing a closing square bracket in $('a[name=birthdayedit'). Also, the name there doesn't match the name on your HTML! Commented Oct 4, 2011 at 22:05

3 Answers 3

1

Change

$('a[name=birthdayedit')

to

$('a[name=topartistsedit]')

or change the name in your HTML.

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

Comments

0

I see several mistakes:

  1. There is not closing bracket (a ]) in a[name=birthdayedit'
  2. The name attribute in the HTML is different from that which the JS references.

1 Comment

Thanks everyone, somehow I missed that closing bracket. It was also stupid of me to not post the CORRESPONDING section of javascript. I just picked one for some reason instead of the right one. Thanks again!
0

It's easier to change your HTML to use an id like this:

<div class="edit"><a href="#" id="topartistsedit">edit</a></div>

And, then you can capture the click like this:

$("#topartistsedit").click(function() {
    // do what you want in the click function
    return(false);   // returning false stops default behavior and propagation
});

or if the content is created dynamically after page load:

$("#topartistsedit").live("click", function() {
    // do what you want in the click function
    return(false);   // returning false stops default behavior and propagation
});

Example here: http://jsfiddle.net/jfriend00/8FgFP/

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.