2

When I do $('#conatiner1 span') on my page, it returns an array with 11 span elements. I also have an array 'vals' with 11 different values.

Is there a fast 'do more, write less' jquery command to easily assign each value of val to each span element?

span 1 -> val 1;
span 2 -> val 2; 
span 3 -> ...
0

1 Answer 1

3

Yes, take a look at jquery each methods. There are few of them and all of them are passing element index to your handler function.

For example, if you store text in values which you want to put into those spans:

var values = [...] // some values
$('#conatiner1 span').each(function(i) { $(this).html(values[i]); });
  1. http://api.jquery.com/each/
  2. http://api.jquery.com/jQuery.each/
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.