Ankit21ks
Ankit21ks

Reputation: 484

accessing values form string in jquery

I am returning string from database in following

format =opRadio=1&selSchool=0&opRadio2=1&selClg=0

What I want to access values like 1 or 0 in javascript or jQuery. I mean I want to retrieve 1 when I pass opRadio. I tried by encoding string into json but no luck.

Thanks in advance.

Upvotes: 0

Views: 31

Answers (1)

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

Try this :You can use split('&') to seperate key value pair and then use split('=') to get key and value to put it in map. use map to retrieve values by passing key

        var dbString = "opRadio=1&selSchool=0&opRadio2=1&selClg=0";
        dbString = dbString.split('&');
        
        var map = {};
        
        for(var i=0;i<dbString.length;i++)
        {
          var keyValue = dbString[i].split('=');
          map[keyValue[0]] = keyValue[1]; 
        }
    
       //to read value by passing key
        alert(map['opRadio']);

Upvotes: 3

Related Questions