Best way to return JSON?

I’m using Ajax.Updater to update a select element, and have just
discovered
that IE doesn’t like having its innerHTML set. I figure instead of a
bunch
of option tags I can return JSON and parse it to add the options, but
I’m
not quite sure of the best way to do this… or any way to do it really
:slight_smile:
Could someone point this out to me? Or am I using a totally antiquated
technique and should just use RJS?

Thanks
Daniel

Daniel H. wrote:

I’m using Ajax.Updater to update a select element, and have just
discovered
that IE doesn’t like having its innerHTML set. I figure instead of a
bunch
of option tags I can return JSON and parse it to add the options, but
I’m
not quite sure of the best way to do this… or any way to do it really
:slight_smile:
Could someone point this out to me? Or am I using a totally antiquated
technique and should just use RJS?

Thanks
Daniel

I ran into the same issue of IE not liking replacements of a select
tag’s innerHTML the other week. The solution I used was just to wrap the
select tag in a block, and replace the entire select tag / the block’s
innerHTML. This is non-optimal, but it was quicker than producing some
JSON and having a javacript event render new select options.

Yeah, I had read that technique but decided it’d be good to get some
experience messing around with JSON, just because. Also because using
JSON
meant rewriting less code. Here’s a JS function I ended up writing, in
case
it helps:

function populateSelectFromJson(select, json)
{
select.innerHTML = ‘’
for (i = 0; i < json.length; i++)
{
pair = json[i]
select.options[i] = new Option(pair[1], pair[0]);
}
}

I wasn’t aware you could do that :slight_smile:

I noticed you also got rid of the temp variable “pair” - how come? I
notice
some people prefer to use a temp variable and some people don’t, but I’m
not
really sure why. Hmm in my case the variable doesn’t really even add
any
extra clarity. In fact “json” isn’t named well either, since it’s the
result of an eval on JSON rather than the JSON itself :stuck_out_tongue: .

Thanks!

function populateSelectFromJson(select, json)
{
select.innerHTML = ‘’
for (i = 0; i < json.length; i++)
{
pair = json[i]
select.options[i] = new Option(pair[1], pair[0]);
}
}

Wouldn’t:

function populateFromJSON(select, json) {
select.options.length = 0;
for (var i = 0; i < json.length; i++) {
select.options[select.options.length] = new Option(json[i][1],
json[i][0]);
}
}

be better? It avoids the innerHTML completely, and leaves space to add
a ‘title’ option. Some managers like that sort of thing :wink:


Phillip H.
http://www.sitharus.com/

I wasn’t aware you could do that :slight_smile:

You can do all sorts in JavaScript, it’s got quite complex over the
years.

I noticed you also got rid of the temp variable “pair” - how come? I notice
some people prefer to use a temp variable and some people don’t, but I’m not
really sure why. Hmm in my case the variable doesn’t really even add any
extra clarity. In fact “json” isn’t named well either, since it’s the
result of an eval on JSON rather than the JSON itself :stuck_out_tongue: .

Personal preference. I use temp variables where a resolution is
expensive (eg $()) or where I’m accessing it several times. My test is
to look at it and see if I’m saving typing or not :wink:


Phillip H.
http://www.sitharus.com/