-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)
000
  • 26,951
  • 10
  • 71
  • 101
user007
  • 3,203
  • 13
  • 46
  • 77
  • 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
  • 103,943
  • 14
  • 161
  • 157
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
  • 1,010
  • 8
  • 12
0
if (dropdown.contains(myname))
    alert(dropdown.value.substring(myname.length +1)
AurA
  • 12,135
  • 7
  • 46
  • 63
Pieter_Daems
  • 1,234
  • 2
  • 14
  • 20