JQuery if optgroup does not contain

I need help with jQuery below, this does not work for me. I want to perform some actions only if <optgroup label="Knowledge Base"> does not exist

My HTML (which cannot be changed):

 <select id="entry_forum_id" name="entry[forum_id]"> <optgroup label="Knowledge Base"> … </optgroup> <optgroup label="Incidents"> … </optgroup> <optgroup label="Articles"> … </optgroup> </select> 

My jQuery attempt:

 if($('optgroup:not([label="Knowledge Base"])')) { alert('test'); } 
+4
source share
2 answers

$('optgroup:not([label="Knowledge Base"])') label = "Knowledge Base"])') returns a jQuery wrapper object, you need to check its length property to see whether it contains any elements

You need to test

 if($('optgroup[label="Knowledge Base"]').length == 0) { alert('test'); } 
+6
source

I don’t think you need a pseudo selector: no. Just check if optgroup is with this label:

 if($('optgroup[label="Knowledge Base"]').length == 0) { 

Fiddle

+5
source

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


All Articles