0
$("#index-link")[0].search = "?isNameChecked=False&isDateChecked=False&isStatusChecked=True"

How can I find the isStatusChecked == true or false from the above string using jQuery?

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
Earth
  • 3,477
  • 6
  • 37
  • 78

6 Answers6

1

I guess there are several ways to do it. One way is str.indexOf("isDateChecked=False") which will return value > -1 if the string is found.

However regex might be a better option if you want to allow for variable spacing in the substrings you're checking for. For fixed strings though, I would avoid regex and go with indexof

TGH
  • 38,769
  • 12
  • 102
  • 135
0

you may need to look at substr function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr

or use search method http://www.w3schools.com/jsref/jsref_search.asp

Dipak Yadav
  • 114
  • 17
0

Found a similar question to this that provided a function which does this here.

By modifying it a bit (assuming you want to use a string of text, not the url from the address bar) I came up with this -

function getParameterByName(name, url) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(url);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

Here's a fiddle with it in action - http://jsfiddle.net/dNe38/

The value is found by using a regular expression as Pranav suggested.

Community
  • 1
  • 1
Pankucins
  • 1,690
  • 1
  • 16
  • 25
0

No jQuery required. Using built-in JavaScript functions:

var a = "?isNameChecked=False&isDateChecked=False&isStatusChecked=True";

var params = a.split("&");

params.forEach(function(param) {
    var keyValue = param.split("=");
    var key = keyValue[0];
    var value = keyValue[1];
    if(key === 'isStatusChecked') {
        alert(value); //True
    }
});

JSFiddle

Works even if you add more parameters to it.

c.P.u1
  • 16,664
  • 6
  • 46
  • 41
  • But it doesn't work if you remove the other parameters, since the `?` interferes. – tom Jul 04 '13 at 05:47
  • 1
    @tom You could use `a = a.substring(1)` first to delete the `?` ([`String.substring` docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring)). – Rory O'Kane Jul 04 '13 at 06:19
0

you can split if the string by isStatusChecked

var newString = asearch.split("isStatusChecked=").pop(-1);
0

Simple regexp will make a job:

var str = "?isNameChecked=False&isDateChecked=False&isStatusChecked=True";
str.match(/isStatusChecked=(.*?)(?:$|&)/); // ["isStatusChecked=True", "True"]
dfsq
  • 191,768
  • 25
  • 236
  • 258