LJ Wiest-Berger
LJ Wiest-Berger

Reputation: 31

Accessing in Javascript link passed from PHP

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?

Upvotes: 0

Views: 80

Answers (3)

Uku Loskit
Uku Loskit

Reputation: 42050

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.

Upvotes: 0

stealthyninja
stealthyninja

Reputation: 10371

Change

$('a[name=birthdayedit')

to

$('a[name=topartistsedit]')

or change the name in your HTML.

Upvotes: 1

jfriend00
jfriend00

Reputation: 708036

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/

Upvotes: 0

Related Questions