Reputation: 3196
I have a number of urls, and I'm wondering if there's a way to dynamically change the last digit of them.
<div class="m_item">
<a class="thumbnail_link" href="http://paraboladesignstudio.ipage.com/yahaira/fashion/fashion-slideshow/?thumb=0">
<img src="<?php echo $image['sizes']['thumbnail'];?>" title="<?php echo $image['title'];?>" alt="<?php echo $image['alt']; ?>">
</a>
</div>
The above code outputs certain number of ".m_item"s with the same "a href"s.
This is my jQuery code:
var i=0;
i++;
$(".thumbnail_link").each(function() {
this.href = this.href.replace("0", i);
});
It changed all the urls to "..../?thumb=1"
How could I make the digit increase? I've tried .children with no luck.
Thank you.
Upvotes: 0
Views: 182
Reputation: 16438
Get rid of the i and just use the index of the each() https://api.jquery.com/each/
$(".thumbnail_link").each(function(index) {
this.href = this.href.replace("0", index);
});
Or you can do this in case there are other 0s in the url
$(".thumbnail_link").each(function(index) {
this.href = this.href.replace("thumb=0", "thumb=" + index);
});
Upvotes: 3
Reputation: 30760
It's doing that because i
is only ever incremented once. It starts out as zero, then you bump it up to 1 with i++
, and then you never change it again. Try this:
$(".thumbnail_link").each(function(i) {
this.href = "http://paraboladesignstudio.ipage.com/yahaira/fashion/fashion-slideshow/?thumb=" + i;
});
Upvotes: 2
Reputation: 3844
or if you want to update the last number, use this
$(".thumbnail_link").each(function(index) {
this.href = this.href.replace(/([\d]+)$/g, index);
});
Upvotes: 1