tags:

views:

132

answers:

4

Hi 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

});
+5  A: 

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
});
strager
Thank you for your valuable comments
learner
@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.
strager
A: 
for (var i = 0; i < element.length; i++) {
var tempVar = element[i];
}

something like this

Wai Wong
A: 

http://stackoverflow.com/questions/943987/how-can-i-make-a-for-loop-in-jquery

This will help you..

I don't think this is related to what the OP wanted.
strager
A: 

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

Sharique