Topic: Ajax select boxes

Consider this code:

<select id="search-field-city" onchange="<%= remote_function(:update => 'search-field-rent', :url => {:action => :update_rent}) %>">
  <option value="selecteer">Kies een stad</option>
  <% for room in @rooms.uniq %>
    <option value="<%= room.city %>"><%= room.city %></option>
  <% end %>
</select>
&nbsp;
<select id="search-field-rent">
  <option value="selecteer">Kies een bedrag</option>
  <% for room in @rooms.uniq %>
    <option value="Huurprijs"><%= room.rent_amount %></option>
  <% end %>
</select>

I have an action defined in my controller called "update_rent", and it simply does a render_text "bla", but when I change the first select box it does empty out the values that are initially in there, but it doesn't show "bla"... Any thoughts?

Thanks in advance,

Danny

Re: Ajax select boxes

actually it does work in firefox, but not in IE... strange, anybody knows what's going on here?

Re: Ajax select boxes

Try putting "option" tag around the text:

render_text "<option>bla</option>"

Railscasts - Free Ruby on Rails Screencasts

Re: Ajax select boxes

yeah I actually have that, was typing to fast for my thoughts and omitted them in the post, sorry... I actually have it working now smile

Re: Ajax select boxes

by the way... how do you make it do the code thing on this forum? just placing text between "<code></code>"?

thanks

Re: Ajax select boxes

Place it between [ code ] [ /code ] tags (without the spaces). For ruby it's [ code=ruby ] [ /code ].

As for the problem, IE is finicky about inserting HTML directly into the select element. You may need to write some custom Javascript to do the job. Something like this.

// change [1] to whatever position you want to set it to
$('search-field-rent').options[1] = new Option("name", "value");

Railscasts - Free Ruby on Rails Screencasts

Re: Ajax select boxes

Or you can rewrite the entire dropdown list instead of just inserting the option, whichever is easier for you.

But yeah, innerHTML doesn't work in tables or selects in IE. sad

vinnie - rails forum admin

Re: Ajax select boxes

vin wrote:

Or you can rewrite the entire dropdown list instead of just inserting the option.

Aww, but that's the easy way out! wink

Seriously though, vin's suggestion is probably the best way to go!

Railscasts - Free Ruby on Rails Screencasts

Re: Ajax select boxes

can't help but wonder what the hard way out is? since I'm ahead on my schedule anyway I have time to experiment... Maybe a link towards some more detailed info, please?

Danny

Re: Ajax select boxes

hmz, missed the clue again... I read over the post of rynb explaining the custom js... trying it out now.