Wednesday, September 12, 2007

Select box manipulation with jQuery - new plugin: selectedValues

A new plugin has been added for getting the values of options selected in a select box. More details and download from the Select Box Manipulation project page at the jQuery site.

5 comments:

rob70 said...

Tried to find an email address for you and couldn't.

FYI:
This plugin has a bug that causes options with duplicate values not to render.

only one of the following options will render:

var dArray = {"none":"one","none":"two"]

Sam said...

I am just using the built in method in jQuery for getting selected options, i.e.

$("#myselect option:selected")

You should be able to get the duplicates with a slight tweak:

$.fn.selectedValues = function()
{
  var v = [];
  this.find("option").each(
    function()
    {
      if(this.selected) v[v.length] = this.value;
    }
  );
  return v;
};


Although why would have multiple options with the same value? Perhaps file this as a bug in jQuery?
http://dev.jquery.com/newticket/

rob70 said...

Actually, the error doesn't occur when getting selected values, it occurs when rendering the values from an array using addOption:

var dArray = {"none":"one","none":"two"]
$("#selectBox").addOption(dArray,false);

panrick said...

I'm using the selectboxes plugin and am seeing 'odd' behavior after calling sortOptions. Is there some way to contact you about what I am seeing?

I have test page and I can send you the url if you like.

Briefly, what I am seeing is that after calling sortOptions a call to addOptions to change the text of an existing option renames the wrong option. Please let me know if I can send you the url for the example. Thank you.

Sam said...

That can occur if you have multiple options with the same value.

You can change values the plain jQuery way: $("#myselect").find("option[value=a]").attr("text","I am a")

Although you may still get the same problem with duplication values.