0

Is there a simple one liner I can use to select the nth element from a comma separated list in jQuery?

For example if I have: var eg = "a,b,c,d";

How can I select element c?

1
  • tip: enclose all code within backticks " ` " so it appears like this: var eg = "a,b,c,d"; Commented Mar 14, 2012 at 1:08

1 Answer 1

5
var eg = "a,b,c,d"; eg.split(",")[2]; 
Sign up to request clarification or add additional context in comments.

3 Comments

ha! I just noticed the OP tagged it as jQuery. Opps. Bring on the downvotes.
jQuery version, for fun: jQuery.split = function(s, d){return s.split(d)} then $.split("a,b,c,d", ",")[2]
@Kyle—cool! More code to just call the function than write the expression!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.