3

I have made a fiddle. http://jsfiddle.net/Zfaf6/

I am experiementing with arrays, but only seem to output the first object.

Can any please advise, thanks.

My jquery

$("a.download-all").on('click', function () {

    var downloads = $('a[data-download]').attr('data-download');

    // This is the example output of the data in the variable...

    $('#varObj').html(downloads);

});


My script

<div id="images">

    <a href="#" data-download="http://image.com/1.jpg"><img src="images/x.gif" alt=""/></a>
    <a href="#" data-download="http://image.com/2.jpg"><img src="images/x.gif" alt="" /></a>
    <a href="#" data-download="http://image.com/3.jpg"><img src="images/x.gif" alt=""/></a>
    <a href="#" data-download="http://image.com/4.jpg"><img src="images/x.gif" alt="" /></a>
    <a href="#" data-download="http://image.com/5.jpg"><img src="images/x.gif" alt=""/></a>
    <a href="#" data-download="http://image.com/6.jpg"><img src="images/x.gif" alt="" /></a>

<div>

<p><a href="#" class="download-all">Download all images</a></p>

<p id="varObj"></p>

http://jsfiddle.net/Zfaf6/

4 Answers 4

5

jQuery's attr() will, as a "getter" (and per the docs) return the value of the first element matched; which is why you're only seeing one.

Either iterate over the match objects using each(), or use map();

$("a.download-all").on('click', function () {

    var downloads = $('a[data-download]').map(function () {
        return $(this).attr('data-download');
    });

    // This is the example output of the data in the variable...
    $('#varObj').html(downloads.get().join(' '));
});
Sign up to request clarification or add additional context in comments.

2 Comments

I think the map() is suited perfectly for this situation.
Good up! Can't believe I didn't think of map.
1

LIVE DEMO

var downloads = [] ;

$("a.download-all").on('click', function () {

    $('a[data-download]').each(function(){   
        downloads.push( $(this).attr('data-download') );        
    });

    $('#varObj').html(downloads);

});

Comments

1

calling attr() of a set will only return the first attr(). Also, use data() as it's easier to read.

So, the code just creates an empty array, then using each we push each item's download value into the array. The rest of your code is the same.

$("a.download-all").on('click', function () 
{
    var downloads = [];

    $('a[data-download]').each(function()
    {
        downloads.push( $(this).data('download') );
    });

    // This is the example output of the data in the variable...

    $('#varObj').html(downloads);
});

2 Comments

Thanks also. And the tip on data, always forget about that.
and the updating my title :-)
1
var downloads = $('a[data-download]').map(function(){   
   return  $(this).data('download');        
});
$("#varObj").html(downloads.join("-"));

3 Comments

You've trimmed my favourite answer down even more!
Though its a bit weird - not working for some reason... jsfiddle.net/Zfaf6/4
typo : download') ) extra bracket

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.