Wednesday, February 14, 2007

Select box manipulation with jQuery

If you read the jQuery mailing list, you may be aware that I have updated my plugins for working with select boxes. Summary of changes:

  • addOption replaces any options with the same value (so think of it as an add/replace options plugin
  • selectOptions selected options in addition to what was already selected, but there is now an option for it not to do this
  • There are two new methods:
    • copyOptions which is for copying options between select boxes
    • ajaxAddOption allows you to add options via AJAX (i.e. could be used for create options dynamically from a server script)


Alex Ezell said...

I would like to use the ajaxAddOption from your "select" plugin. The problem is that I really want to replace all the options currently in a particular select with the ones which come in the JSON response.

So, does it make sense to add some functionality to removeOption to be able to remove everything and then call ajaxAddOption to add in the new options?

Sam said...

removeOption can now take a regular expression. So download the new version, and you can do:

$("#myselect").removeOption(/./).ajaxAddOption ("myoptions.php");

Anonymous said...

How about a moveOptions method that moves all selected options from one select to another?