0

I'm new to JavaScript, jQuery and Stack.

I'm trying to get the url with data().url and add the same as src to each img. Here is an example of the HTML.

<ol>
  <li><div class="image"><img data-url="https://lh4.googleusercontent.com/.../s400/image-1.png"/></div></li>
  <li><div class="image"><img data-url="https://lh4.googleusercontent.com/.../s400/image-2.png"/></div></li>
  <li><div class="image"><img data-url="https://lh4.googleusercontent.com/.../s400/image-3.png"/></div></li>
</ol>

How this can be achieved with jquery?

4 Answers 4

1

Try this:

$(".image img").each(function(){
    $(this).attr("src" , $(this).data().url );
});

Here is the example: http://jsfiddle.net/42A93/1/

Welcome to SO!

Sign up to request clarification or add additional context in comments.

1 Comment

Oh... my bad, I was doing it wrong for var while changing the size of the image. However, thank you all for the help... and @Cherniv, it worked perfectly...
1

You could do this :

$('.image img').attr('src', function(){ return $(this).data('url') });

Comments

0

Try

$(".image img").attr('src', function(){
    return $(this).data('url');
})

Comments

0
$( '.image img' ).map( function(){
     this.setAttribute('src', this.getAttribute('data-url'));
});

Comments

Your Answer

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.