-1

I have a string:

title=Hello world&dropdown=on&count=on&hierarchical=on

and I want to explode this string from &, Like;

title=Hello world
dropdown=on
count=on
hierarchical=on

I already know the this names title, dropdown, count,hierarchical (but this names are not fixed, it can be anything),

Now I want to match this names (name means words before equal to sign) to name I know, if match then get value (words after equal to sign), like:

if (name == myname) then get value

var myname = dropdown;
if (dropdown == myname)alert(dropdown.value)
Joe Frambach
  • 26,300
  • 10
  • 69
  • 98
user007
  • 3,025
  • 13
  • 44
  • 76
  • 1
    I don't really understand your question but you can use `split` like: `"title=Hello world&dropdown=on&count=on&hierarchical=on".split("&")` – putvande Jul 17 '13 at 13:35

3 Answers3

3

Split the string into an array, then put that array into an object, then you can compare nicely:

var string = "title=Hello world&dropdown=on&count=on&hierarchical=on";
var stringArr = string.split("&");
console.log(stringArr);

var newObj = {};

for (var i = 0; i < stringArr.length; i++) {
    var parts = stringArr[i].split("=");
    newObj[parts[0]] = parts[1];
}

console.log(newObj);

Now, newObj.title will equal Hello World. Hope this helps.

Oh, and a fiddle: http://jsfiddle.net/KXMLp/

tymeJV
  • 102,126
  • 13
  • 159
  • 155
0

split on & to create an array of strings, then split on = to get your name value pair. name is first element[0], value is second element[1]

tmcc
  • 992
  • 7
  • 12
0
if (dropdown.contains(myname))
    alert(dropdown.value.substring(myname.length +1)
AurA
  • 11,845
  • 7
  • 47
  • 63
Pieter_Daems
  • 1,104
  • 2
  • 12
  • 20