Search code examples
javascriptjqueryclickrel

get the value of rel attribut of a link on click event of link using jquery


I have many link like this with different rel attributes

<a href="#" class="buy" rel="WVSEU1Y">buy</a>

I want to get the value of rel attribute on click... but this code doesn't seem to work but firebug also doesn't fire any error in console. What am i doing wrong?

$("a.buy").click(function(event) {
event.preventDefault();
var msg =  $(this).attr('rel');
alert(msg);

});

update: I corrected the html error i had. Its not preventing default. And the click event doesnt seem to work.

Adding the code top of all the other scripts worked.


Solution

  • It is working fine here http://jsfiddle.net/niklasvh/NFfe5/ but note that you have an error in your HTML, you are missing the quotation mark around href=#"

    Make sure your code is wrapped in a DOM ready as well:

    $(function(){
    // your code
    });