How do i trim and get the value after a special character from a hidden field The hidden field value is like this
Code
//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);
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();
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>
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 ?