0

i have this static code that will hide/show some combo box but what if i add more category´s ?well if do add more category´s i have to change the code every time

so what i want to do is have a var that will receive several values separated by comma

and them some how it will separate the values and them it will store the values in a array. and now when the user needs to add more category´s i don't have to edit the code.

but how can i separate the values divided by a comma and then add them to a array?

1
  • 'bla,bla,bla'.split(',') -> ['bla', 'bla', 'bla'] Commented Apr 12, 2012 at 15:09

4 Answers 4

1

If I understand your question correctly you'll want to have a look at http://www.w3schools.com/jsref/jsref_split.asp

And use var arr = YOURVARIABLE.split(',');

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the String.split() function, e.g:

var s = '1,2,3,4,5,6';
var values = s.split(',');

console.log(values);

For more information see here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

3 Comments

in this exemple i need to know what the values of "s" will be?
You may need to explain a bit further. You have a lot of good answers here to the question you've asked, if there is something more you are trying to achieve then please make it very clear. :)
They will be strings. '1,2,3,4' split becomes ['1','2','3','4']
0

You would use the split() method. http://www.w3schools.com/jsref/jsref_split.asp

var string = '1,2,3,text,123';
var array = string.split(',');

//array = [1, 2, 3, 'text', 123];

Comments

0

Try the following code

var testString = "comma,seperated,list";

var stringArr = testString.split(",");

the split() method will literally split the string by the delimeter passed to it and return an array of values. in this case our array will be

// stringArr = ["comma", "seperated", "list"];

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.