Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi
I have created an array of the id's of the list item and i would like to know how would i be able to loop through the array and get the index of the array using Jquery as i would like to only update the class of the selected ids which are in the array list

localStorage.selectableList = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]"

var list = JSON.parse(localStorage.selectableList);


That is how i have created my array
Posted
Updated 6-Oct-15 23:54pm
v2

You can do this like

JavaScript
// Your array of ids
var arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]

// Loop through the array
$.each(arr, function (i, val) {
    // Add class to the respective ids from array items
    $('#' + val).addClass('active');
});


Fiddle[^]
 
Share this answer
 
v2
You mean this ?
JavaScript
for(var i=0; i<list.length;>{
    console.log(i +"\n---");
}

And you never get the index of the array, you the index of the item of the array.
var index = list.indexOf("2");
indexOf()[^]

-KR
 
Share this answer
 
Try like below:
JavaScript
var list = JSON.parse(localStorage.selectableList);
for (var i = 0; i < list.length; i++) {
	$('#' + list[i]).addClass('yourcssclass');
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900