visionInc
visionInc

Reputation: 325

How to create an array with <input>?

I want to create an array with a html element. My main problem is, that i just get a string inside an array but no array.

Example: input is: 1,5,7,3,4 inside an array it look like this: ["1,5,7,3,4"] but i want it like this: [1,5,7,3,4]

I look for a solution made in javascript or better in jquery!

Upvotes: 0

Views: 101

Answers (2)

Andy
Andy

Reputation: 63524

Use the split function to convert the string to an array.

For example:

<input type="text" id="myInput">1,5,7,3,4</input>

var myInput = $('#myInput').val();
var arr = myInput.split(','); // [1,5,7,3,4]

Upvotes: 1

Quentin
Quentin

Reputation: 943209

split will convert a string to an array.

var myArray = document.getElementById('myInput').value.split(',');

Upvotes: 3

Related Questions