Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value from a string after a special character

Tags:

jquery

parsing

How do i trim and get the value after a special character from a hidden field The hidden field value is like this

Code

<input type=-"hidden" val="/TEST/Name?3" 

How i get the value after the "question mark" symbol in jquery??

like image 640
Sullan Avatar asked Nov 20 '10 11:11

Sullan


People also ask

How do you get the string after a certain character?

To get the substring after a specific character, call the substring() method, passing it the index after the character's index as a parameter. The substring method will return the part of the string after the specified character. Copied! We used the String.

How do you slice a string after?

“javascript slice string after character” Code Answer'svar string = "55+5"; // Just a variable for your input. character as a delimiter. Then it gets the first element of the split string.


2 Answers

You can use .indexOf() and .substr() like this:

var val = $("input").val(); var myString = val.substr(val.indexOf("?") + 1) 

You can test it out here. If you're sure of the format and there's only one question mark, you can just do this:

var myString = $("input").val().split("?").pop(); 
like image 184
Nick Craver Avatar answered Oct 13 '22 08:10

Nick Craver


Assuming you have your hidden input in a jQuery object $myHidden, you then use JavaScript (not jQuery) to get the part after ?:

var myVal = $myHidden.val (); var tmp = myVal.substr ( myVal.indexOf ( '?' ) + 1 ); // tmp now contains whatever is after ? 
like image 32
Jan Hančič Avatar answered Oct 13 '22 10:10

Jan Hančič