I'm trying to move strings from name attributes to the src of each img. I'm using jQuery to do this:
$("#one").find("img").attr("src",$(this).attr("name");
Now the problem is that $(this) is not the current manipulated element. So how do I get the current element that find() has found?
Nelson Benítez León
51.1k8 gold badges70 silver badges84 bronze badges
asked Oct 23, 2012 at 18:57
Lahey
2312 gold badges4 silver badges11 bronze badges
1 Answer 1
attr method accepts a function, within the context of this function, this refers to the current element:
$("#one").find("img").attr("src", function(){
return this.name
});
answered Oct 23, 2012 at 18:58
Ram
145k16 gold badges174 silver badges201 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Lahey
Oh yes of course. I thought it would be possible without creating a local function but it works, the img is now visible :) Thanks!
Selvakumar Arumugam
@Lahaye Note:
.attr is just going to pick the first image.. it will not update thru all the image that the find going to return. Get the value of an attribute for the first element in the set of matched elementsRam
@Vega That's not the case with
attr's function.lang-js
namecoming from? the#oneor theimg?