1

I want to select all child elements of OPTGROUP when user clicks on OPTGROUP inside Select box. But for some reason my code is not working. Click event on OPTGROUP is not triggering.

Since it's multiselect element I am using Bootstrap Multiselect plugin

Here is my code:

$("optgroup.opt_group").select(function(e) {
    $(this).children().attr('selected', 'selected');
});

<select id="example19" multiple="multiple">
    <optgroup label="Mathematics" class="opt_group">
        <option value="analysis"> Analysis</option>
        <option value="algebra">Linear Algebra</option>
        <option value="discrete"> Discrete Mathematics</option>
        <option value="numerical">Numerical Analysis</option>
        <option value="probability"> Probability Theory </option>
    </optgroup>
    <optgroup label="Computer Science" class="opt_group">
        <option value="programming"> Introduction to Programming</option>
        <option value="automata">Automata Theory</option>
        <option value="complexity"> Complexity Theory</option>
        <option value="software">Software Engineering</option>
   </optgroup>
</select>
Gilsha
  • 14,431
  • 3
  • 32
  • 47
jureispro
  • 1,342
  • 5
  • 22
  • 43

1 Answers1

3

It is because you are not using click event try this:

$("optgroup.opt_group").click(function(e) {

      $(this).children().attr('selected','selected');
    });
Brijesh Bhatt
  • 3,810
  • 3
  • 18
  • 34