AlexC
AlexC

Reputation: 9661

Select word from value

I have input

<input type="hidden" value="Select Value 11121 DataValue " name="111" id="222"/>

I select value with jQuery

alert( $('#222').val() );

but

how can I choose only "11121" from this input value with jQuery ?

I can create a filter ?

Upvotes: 0

Views: 116

Answers (2)

jeroen.verhoest
jeroen.verhoest

Reputation: 5183

You could use a javascript regex to get the numeric value out of that string:

var regex = new RegExp(/\d{5}/igm);

var myNumericValue = regex.exec($('#222').val());

Upvotes: 1

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107518

Would it make more sense to create four inputs that are hidden, each one with one of the values from the one

in your post? I would do that rather than try to parse out certain strings from a single input value. Just give each a unique id and go from there.

If you really want to keep one, you could do the following:

<input type="hidden" value="Value1 Value2 Value3 Value4" id="some_id" name="some_name" />

and pluck out the values with:

var vals = $('#some_id').val().split(/\W+/);

and then access them by position:

var val1 = vals[0]; // "Value1"
var val2 = vals[1]; // "Value2"
...

Upvotes: 1

Related Questions