Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check with jquery if select has options?

Tags:

How to check with jquery if select has options and if it doesn't to disable it and show [not items]?

like image 979
George Avatar asked Feb 11 '11 17:02

George


People also ask

How do you check if a select has an option in jQuery?

Show activity on this post. if ($("#yourSelect"). find('option[value="value"]'). length === 0){ ... }

How do I know which option is selected in dropdown?

Answer: Use the jQuery :selected Selector You can use the jQuery :selected selector in combination with the val() method to find the selected option value in a select box or dropdown list.

How check dropdown is empty or not in jQuery?

The Best Answer isval() === "") { // ... }


2 Answers

One way to do to it:

if ($('#myselect option').length == 0) {     $('#myselect').hide(); } 
like image 74
Nathan Anderson Avatar answered Dec 10 '22 16:12

Nathan Anderson


var $select = $('input:select option'); if(!$select.length )     $select.attr('disabled', true); 

Or:

$('select:not(:has(option))').attr('disabled', true); 
like image 37
jAndy Avatar answered Dec 10 '22 17:12

jAndy