0

Is there any way to do something like this to avoid having to do .each or a for loop?

$('.backstretch-frame').backstretch( function(){ return $(this).data('img'); } );

Basically trying to pull the contents of the attribute for that particular object in the collection for instantiation.

5
  • It's not very clear what your intended result is. Are you looking for an array of strings which are the img data attribute of each .backstrecth-frame? Commented May 16, 2013 at 20:00
  • In a nutshell, I'd like to apply backstretch() to all instances of .backstretch-frame using an inline getter instead of having to set up a for loop or using jQuery's .each() functionality. Commented May 16, 2013 at 20:01
  • 1
    Sorry, but .data can only return one value for one element. Why don't you want to use .each? This is exactly the sort of thing you need it for. Commented May 16, 2013 at 20:03
  • 1
    What is backstretch? Why don't you want to use a loop? There's nothing wrong with .each; jQuery uses it internally when you do something like $('.multi').attr('abc', 123);! Commented May 16, 2013 at 20:04
  • Thanks for the help guys - I think I'll just do the each loop. I don't think I was putting my question correctly, nbd. Commented May 16, 2013 at 20:09

1 Answer 1

1

Like it or not, if you want to get ALL the .data('img') values for each instance of .backstretch-frame, you'll need an .each loop:

var temp_array = [];
$('.backstretch-frame').each(function() {
    var t = $(this).backstretch(function() { 
        return $(this).data('img');
    });
    temp_array.push(t);
});
// now temp_array contains all the values of .data('img'), in order
Sign up to request clarification or add additional context in comments.

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.