-1

Possible Duplicate:
How do I split this string with JavaScript?

My point is something like this that I have a string and i have stored it into a variable somethink like this

var text1 = "show, text, into input";

Can I show this variable into three different inputs which are in HTML? I mean every word after ", " should be show into an input.

Community
  • 1
  • 1
Husnain Ahmed
  • 135
  • 1
  • 1
  • 12

3 Answers3

0
var split = 'show, text, into input'.split(',');

var first= split[0];
var second = split[1];
Moiz
  • 2,409
  • 5
  • 27
  • 50
0
var parts = text1.split(','),
    nodes = document.querySelectorAll('input[class="txt"]');

for (var i = nodes.length; i--, nodes[i].value = parts[i];);​

And the HTML:

<input type="text" class="txt" />
<input type="text" class="txt" />
<input type="text" class="txt" />
David G
  • 94,763
  • 41
  • 167
  • 253
0

Try something like this:-

1) Use .slice() to get the first two characters and then the characters.

2) Use document.getElementById() to fetch your boxes. (Make sure they have IDs)

3) Assign the strings to the .value property of the boxes.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331