user2238083
user2238083

Reputation: 591

Creating an Array JS

I create a variable from a title attribute tile="Red, Blue". Is there a simple way to make this an array. So the array has 2 values "Red" & "Blue"?

Upvotes: -1

Views: 81

Answers (3)

Brian Ogden
Brian Ogden

Reputation: 19212

This is what you are looking for:

var myArray = new Array("Red","Blue");

Upvotes: -1

Sergio
Sergio

Reputation: 6948

Do you mean splitting string?

var title = "Red, Blue";
var titleArray = title.split(", ");

Upvotes: -1

AllTooSir
AllTooSir

Reputation: 49372

You can create an array as :

var title = "Red, Blue";
var array = tile.split(',');

OR ,

var array1 = document.getElementById('yourElementID')
                      .getAttribute('tile').split(',');

Upvotes: 0

Related Questions