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
Daniel Higginbotham 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 > DanielI 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. -- Posted via http://www.ruby-forum.com/.
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]); } } -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Gardiner Allen Sent: Wednesday, May 24, 2006 11:52 AM To: rails@lists.rubyonrails.org Subject: [Rails] Re: best way to return JSON? Daniel Higginbotham 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 > DanielI 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. -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
> 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 Hutchings http://www.sitharus.com/
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 :P . Thanks! -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Phillip Hutchings Sent: Wednesday, May 24, 2006 2:48 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] Re: best way to return JSON?> 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 Hutchings http://www.sitharus.com/ _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
> 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 :P .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 Hutchings http://www.sitharus.com/