1

I have a text input field like this:

someone types = 22x32x5

Is it possible to extract this value into 3 different text input fields without the x in jQuery? And how?

FieldB=22
FieldC=32
FieldD=5

Thanks in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
robroy111
  • 27
  • 8
  • 3
    Possible duplicate of [Convert comma separated string to array](http://stackoverflow.com/questions/2858121/convert-comma-separated-string-to-array) – CuriousSuperhero Nov 17 '15 at 12:42

3 Answers3

2

That can be done using the 'split' command and then putting the array elements in the relevant fields:

array=input.split('x');
$('#input1').val(array[0]);
.....

Here is a working FIDDLE

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
0

Yes it is possible with the function split() it splits a string into an array, use this code:

var types = "22x32x5";
var temp = new Array();
// this will return an array with strings "22", "32", "5", etc.
temp = types .split("x");

for (a in temp ) {
     // Your Code here to populate to input field. You will get the value from temp[a].
}
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
Rajan Goswami
  • 769
  • 1
  • 5
  • 17
0

You could use:

var string =  '22x32x5';
var newstring = string.split('x');
 $('#FieldB').val(newstring[0]);
 $('#FieldC').val(newstring[1]);
 $('#FieldD').val(newstring[2]);

DEMO FIDDLE

Amit Singh
  • 2,267
  • 4
  • 25
  • 50