Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selector in jquery - get rel attribute

I have html code like this:

<li id="vol" rel="1">
    // something here
</li>
<li id="vol" rel="2">
    // something here
</li>
<li id="vol" rel="3">
    // something here
</li>

I'm try to get rel attribute by this code:

$('#vol').click(function(){
  var rel = $(this).attr('rel');
});

But it's not working. I also change this to #vol, but it didn't work. What should i do?

like image 701
kamal Avatar asked Mar 21 '26 15:03

kamal


1 Answers

By Not working, I'm assuming that any li you click, gives you back 1, this is caused by having multiple ID in a page. IDs are supposed to be unique, use class instead.

try

<li class="vol" rel="1">
    // something here
</li>
<li class="vol" rel="2">
    // something here
</li>
<li class="vol" rel="3">
    // something here
</li>

JS:

$('.vol').click(function(){
  var rel = $(this).prop('rel');
});
like image 63
Andreas Wong Avatar answered Mar 24 '26 03:03

Andreas Wong