Get value from a string after a special character

JqueryParsing

Jquery Problem Overview


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??

Jquery Solutions


Solution 1 - Jquery

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();

Solution 2 - Jquery

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 ?

Solution 3 - Jquery

Here's a way:

<html>
    <head>
        <script src="jquery-1.4.2.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                var value = $('input[type="hidden"]')[0].value;
                alert(value.split(/\?/)[1]);
            });
        </script>
    </head>
    <body>
        <input type="hidden" value="/TEST/Name?3" />
    </body>
</html>

Solution 4 - Jquery

//var val = $("#FieldId").val()
//Get Value of hidden field by val() jquery function I'm using example string.
var val = "String to find after - DEMO"
var foundString = val.substr(val.indexOf(' - ')+3,)
console.log(foundString);

Assuming you need to find DEMO string after - by above code you can able to access DEMO string substr will return the string from whaterver the value indexOf return till the end of string it will return everything.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSullanView Question on Stackoverflow
Solution 1 - JqueryNick CraverView Answer on Stackoverflow
Solution 2 - JqueryJan HančičView Answer on Stackoverflow
Solution 3 - JqueryBart KiersView Answer on Stackoverflow
Solution 4 - JqueryGaneshView Answer on Stackoverflow