views:

63

answers:

1

I am not too savvy with JSON so any help would be great...

I am loading a page via JQuery's .load.

I am setting varname as a variable, but it is posting as the actual variable name not what I set it to.

In other words it is showing up as "varname" instead of "class_id".

var varname = "class_id";

$(this).siblings(".container").load(loadurl,{varname:openedid,"all_ids":allids});

Firebug Screenshot

+3  A: 

It's because (AFAIK) you cannot put expression as an ID in the hash constructor, because they're automatically quoted as strings.

In other words, this is valid JavaScript

$(this).load(url, {name: "Foo", age: 13});

The keys name and age are there. In JavaScript you don't have to quote the keys of a hash, although in strict JSON the quoting is necessary for keys. (But the quoting wouldn't hurt. And in some cases, as Jordan suggested in the comments, it's necessary e.g. you want to use reserved words like var as the hash key)

To accomplish your desired effect, I would suggest a lengthy solution (any clever one-liner anyone?)

var varname = "class_id";

var data = {all_ids: allids};
data[varname] = openedid;

$(this).siblings('.container').load(loadurl, data);
kizzx2
Quoting the keys is necessary in some cases--for example IE6 falls over if you use a reserved word as a key, so if you try { class : "some-value" } it will puke because "class" is a reserved word. I think IE7+ and other modern browsers are relieved of this affliction.
Jordan
clever one-liners? we do not aim to achieve *clever* code. you've posted the *proper* way to do it. remember that it's easier to write code than to debug it, so if you're writing as clever code as you can, you won't be clever enough to debug it yourself later.
David Hedlund
@David Hedlund: I agree; though I think all of us here are secretly thrilled when they think of clever one-liners. C'mon, admit it :p
kizzx2
Thanks for the info! You pointed me in the right direction.