Search code examples
jqueryselectoptgroup

jQuery optgroup and option value in multiple select box


Please refer to my first question by this link, thanks to @A.V for helpfulness,

The script which provided is work great in fact, but there is an issue in the giving name for option value and optgroup, please go http://jsfiddle.net/NZ6tY/7/ for more specific action, group 1-1 is working nice with option value and optgroup label without space, comma or slash, where the rest is unworkable, can anyone please give a solution? I must having a long text in the option value and optgroup label as necessary info.

Thanks.


Solution

  • Referencing your jsfiddle, the problem with it is the selector for the optgroup.

    It currently looks like this:

    $('optgroup[label='+somelabel+']');

    Which does work fine, however when somelabel contains spaces or special characters, they interfere with the selector. To remedy this, simply enclose somelabel with quotes, like this:

    $('optgroup[label="'+somelabel+'"]');

    I forked your fiddle and made the update (line 33): http://jsfiddle.net/S7NkB/

    Edit: please note that if somelabel contains quote characters you would want to escape or replace those or it will break again.