Rahul Hedaoo
Rahul Hedaoo

Reputation: 153

How to append bunch of option to select tag in HTML using prototype js

How to append bunch of option to select tag in HTML using prototype js. Thank you.

Upvotes: 0

Views: 109

Answers (1)

Walter
Walter

Reputation: 1150

With a reference to the select tag:

var select = $('my_select_tag_id');

Iterate over your options in a hash:

$H({foo: 'bar', boo: 'baz'}).each(function(pair){
  select.options[select.options.length] = new Option(pair.value, pair.key);
});

It is important that you don't try to just shove the values into the option as if they were HTML elements, using select.insert('<option value="foo">bar</option>') or similar, because that won't work in all browsers. jQuery does something fancy to work around this, because that's precisely the way they would have you do it.

Upvotes: 3

Related Questions