Check if optgroup exists by id / label in jquery?

I have a drop down list and in another process add options / options to this drop down menu and this part works fine.

But I may need to add similar opts groups with a lot of data, and I want to check if this optgroup exists, and if it exists, do not add it, but simply add parameters to the existing optgroup.

I searched around and can't find any help choosing optgroup.

My existing drop-down list has the identifier "user_search_select" and the possible optgroup values ​​may look like "dept_name" or "loc_name" or "last_name"

So, after I add more data, I may need to sort the parameters inside each optgroup, is this possible and how to do it?

Thanks!

+4
source share
1 answer

Check this option . Although I haven't tried it, it looks like this might do the sorting job.

On the verification of existence. Basically you can check:

<script type="text/javascript"> $(document).ready(function() { if($('#mySelect optgroup[label=New group]').html() == null){ $('#mySelect').append('<optgroup label="New group"></optgroup>'); $('#mySelect optgroup[label=New group]').append('<option value="1">New element</option>'); } else { $('#mySelect optgroup[label=New group]').append('<option value="1">New element</option>'); } }); </script> 

The script checks if there is an opt group with a New Lab group, if there is no such group, add a new group, and then add an option to this group. Part of else, just add the parameter to the existing new group.

Then you should apply sorting with the plugin.

+6
source

Source: https://habr.com/ru/post/1316462/


All Articles