I have one text element and button element. I write 12,34,679 to text element, then press to button. It must alert() the first item of the array, generated from text element. How can I do it? Thanks!
3 Answers
Assuming that you want to create an array by splitting on commas you can do the following
var text = getTheText();
var array = text.split(',');
var first = array[0];
alert(first);
2 Comments
Andy
+1 for breaking it down into understandable steps for a beginner.
Ziyaddin Sadygly
Thanks, @JaredPar! You wrote first, so, I will accept your answer! ;)
Use the split method.
<input type="text" id="textElement">12,34,679</input>
var text = document.getElementById('textElement');
var firstIndex = text.split(',')[0];
alert(firstIndex);
myString.split(',')