Reputation: 425
I have a comma seperated list
var list = 1,2,3,4,5,6
Can I use jquery to get the value of list item 3 (for example)
Thanks in advance.
Upvotes: 0
Views: 358
Reputation: 1767
You don't need jQuery. You could do it like:
var item = list.split(',')[3]; //Gets 4th item
Edit
As other people have pointed out below, it might be better to have the list in an array in the first place.
var list = [1,2,3,4,5,6];
//Rather than
var list = '1,2,3,4,5,6';
Although having it as a string then splitting does have some benefits:
var list = '1,2,3,4,5,6'.split(",");
Upvotes: 0
Reputation: 1023
I don't know why you want to use jQuery for this.You can do something like that;
var myArray=["1","2","3"];
and get value with myArray[i]
Upvotes: 0
Reputation: 2527
The proper way of define a list in javascript should be
var i = [1,2,3,4,5,6]
after that, you can just retrieve the value using i[3], which is 4. You don't really need to use jQuery as javascript support it by default
Upvotes: 0
Reputation: 6720
jQuery... why not native javascript functions?
If your list is a string with a comma deliminator...
var list = "1,2,3,four,five,six";
var third = list.split(",")[2];
Otherwise..
var list = [1,2,3,4,5,6];
var third = list[2];
jQuery is moreso used for programability of DOM elements and data objects.
Upvotes: 2