Still trying to learn the basic of jquery so in the weekend I started looking on simple rewriting of links in greasemonkey. The script is working but it’s not looping.
It just take the url from the first .img and and write it into all the other .img instead of getting the each link and write it into the same element. Not sure if it made any sense but take a look at the script and I’m sure you understand. 🙂
function rewrite() {
$.each($(".img"),function(){
var a=$("a img[src*='/SAN/']").attr('src');
$("a img[src*='/SAN/']").parent().attr('href','http://somesite.com/'+a);
});
}
It looks like this is what you’re after:
This loops through each image and sets the parent
<a>hrefproperty based on thesrcof the current image you’re looping over, usingthisinside that.each()loop is the key here. Otherwise.attr()gets the attribute from the first element it matches, rather than the current element you’re looping over.