1

I have a comma separated string. I split that string and assigned it to elements var. How can I loop that elements var?

$(document).ready(function(){   

    var element = $('#imageIds').val().split(","); 

    // how to loop this elements using jquery

});

4 Answers 4

7

You can use a standard loop like this:

var element = $('#imageIds').val().split(",");
var i;

for(i = 0; i < element.length; ++i) {
    // Do stuff with element[i]
}

Or you can wrap element as a jQuery object and use .each or .map or similar:

$($('#imageIds').val().split(",")).each(function(index, value) {
    // Do stuff with value
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your valuable comments
@learner, On the left of each answer, there is a check. Click it to mark that answer as accepted; i.e., that answer answered your question. Please do this with some of your other questions, too. Thanks.
0
for (var i = 0; i < element.length; i++) {
var tempVar = element[i];
}

something like this

Comments

0

Add jQuery click handler to mulitple elements?

This will help you..

1 Comment

I don't think this is related to what the OP wanted.
0

You can use each function of jquery. It is used to iterate through element of an object. Here is nice tutorial.

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.