Reputation: 978
I have a field named tags. The input to this field can be any number of strings,depending on the user.And I need to store those strings in separate variable names. I tried something like this:
var tagsInputArray = ["a", "b"......n elements];
var tagsLength = tagsInputArray.length;
var count = 0;
for (count; count < tagsLength; count++) {
var tags[count] = tagsInputArray[count];
}
This is not working. How can I do it in jQuery?
Upvotes: 2
Views: 52
Reputation: 148120
length
is not defined anywhere you probably need to use tagsLength
in loop
for(count;count<tagsLength ;count++){
You also have error for var tags[count] = tagsInputArray[count];
as this is wrong syntax for declaring array. Removing the var key word would remove the error and your code would work.
var tagsInputArray = ["a", "b"];
var tagsLength = tagsInputArray.length;
var count = 0;
var tags = [];
for (count; count < tagsLength; count++)
tags[count] = tagsInputArray[count];
for (count = 0; count < tags.length; count++)
alert(tags[count]);
I would also recommend to use .push to add array element to other array, see this demo.
tags.push(tagsInputArray[count]);
Upvotes: 2
Reputation: 87203
Try this:
var tagsInputArray = ["a", "b"......n elements];
var tags = [];
for (var count = 0; count < tagsInputArray.length; count++) {
tags[count] = tagsInputArray[count];
}
console.log(tags);
The problem with your code is that
var tags[count] = tagsInputArray[count];
inside for
loop, tags
is undefined
and you are adding elements inside it. I've solved it by defining tags
as array before for
loop and then inserted elements inside it.
Upvotes: 0