4

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
1
  • Where's the name coming from? the #one or the img? Commented Oct 23, 2012 at 18:59

1 Answer 1

6

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
Sign up to request clarification or add additional context in comments.

3 Comments

Oh yes of course. I thought it would be possible without creating a local function but it works, the img is now visible :) Thanks!
@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 elements
@Vega That's not the case with attr's function.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.