# Best way to return JSON?

**URL:** <https://www.ruby-forum.com/t/best-way-to-return-json/61527>\
**Category:** Rails\
**Created:** [May 24, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/best-way-to-return-json/61527 "2006-05-24T19:58:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [May 24, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/best-way-to-return-json/61527/1 "2006-05-24T19:58:01Z")

</div>

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  
🙂  
Could someone point this out to me? Or am I using a totally antiquated  
technique and should just use RJS?

Thanks  
Daniel

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [May 24, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/best-way-to-return-json/61527/2 "2006-05-24T23:52:11Z")

</div>

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  
> 🙂  
> 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.

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [May 25, 2006, 2:41am UTC](https://www.ruby-forum.com/t/best-way-to-return-json/61527/3 "2006-05-25T02:41:25Z")

</div>

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]);  
}  
}

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [May 25, 2006, 2:59am UTC](https://www.ruby-forum.com/t/best-way-to-return-json/61527/4 "2006-05-25T02:59:40Z")

</div>

I wasn’t aware you could do that 🙂

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 😛 .

Thanks!

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [May 25, 2006, 2:50am UTC](https://www.ruby-forum.com/t/best-way-to-return-json/61527/5 "2006-05-25T02:50:33Z")

</div>

> 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 😉

–  
Phillip H.  
[http://www.sitharus.com/](http://www.sitharus.com/)

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [May 25, 2006, 3:05am UTC](https://www.ruby-forum.com/t/best-way-to-return-json/61527/6 "2006-05-25T03:05:51Z")

</div>

> I wasn’t aware you could do that 🙂

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 😛 .

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 😉

–  
Phillip H.  
[http://www.sitharus.com/](http://www.sitharus.com/)
