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
  • 704,970
  • 168
  • 1,303
  • 1,425
robroy111
  • 27
  • 7
  • 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,401
  • 14
  • 78
  • 121
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].
}
Bas van Stein
  • 10,298
  • 3
  • 25
  • 62
Rajan Goswami
  • 761
  • 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

jGupta
  • 2,243
  • 4
  • 22
  • 49