Reputation: 2265
I have a html.twig file with this
{% set num= 1%}
{% for idea in ideas %}
<div class="add">
<a href="#"> Comentar </a>|<a href="#" rel="like{{num}}"> Tomado </a>|<a href="#">Valorar</a>
</div>
</div>
{% set num= num +1 %}
{% endfor %}
so depend of the number of ideas I have more o less likes, like this
<a rel="like1" href="#"> Tomado </a>
<a rel="like2" href="#"> Tomado </a>
<a rel="like3" href="#"> Tomado </a>
If I want to do a click in jquery,but how I indicate the number of link, the value of num variable
$("a[rel='like']").click(function(){
alert($("a[rel='like']").text());
});
any idea!
Upvotes: 1
Views: 88
Reputation: 1046
I am going to assume you want the value of the rel
attribute.
If that's true, you should try this: http://jsfiddle.net/kakashi/Uqqpr/
My solution uses the attr
method to obtain the value of the rel
attribute.
Update It appears you want the number contained in the rel attribute, i.e. you want "1" when it's "like1", "2" when it's "like2", etc. If so, this solution will do that:
http://jsfiddle.net/kakashi/Uqqpr/1/
*Update 2 -- accounting for multiple digit "likes" *
http://jsfiddle.net/kakashi/Uqqpr/2/ This version uses regular expressions to capture the number at the end.
Upvotes: 2
Reputation: 342655
You can use the startsWith
selector like this:
$("a[rel^='like']").click(function(){
var theNumber = this.rel.replace(/[^0-9]/g, '');
});
Chances are, you don't even need to parse the number out of your rel attribute (plus you should really be using a data attribute for that):
$("a[rel^='like']").click(function(){
var theNumber = $(this).index();
});
Example using data
attribute:
<a href="#" data-num="{{num}}">
$("a[rel^='like']").click(function(){
var theNumber = $(this).data("num");
});
Upvotes: 1
Reputation: 176916
for this you can tryout this jquery selector jQuery('[attribute^="value"]')
code it like this
$("a[rel^='like']").click(function(){
alert($(this).attr("rel"));
});
Upvotes: 2