Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector for select elements with empty value attributes?

I'm using the below selector to get all the form inputs that are pre-populated with a value.

$('input[value!=""]');

This works great and so I thought I could apply the same logic to my select elements using the following selector, though it seems to be selecting all the select elements in Chrome.

$('select[value!=""]');

Below is an example of two types of selects on the form:

<select name="phone2_type" id="phone2_type">
  <option value="">Select one:</option>
  <option value="HOME">Home</option>
  <option value="CELL">Cell</option>
  <option value="BUSN">Business</option>
</select>

<select name="state" id="state">
  <option value="">Select one:</option>
  <option value="XX">Outside USA/Canada</option>
  <option value="AL" selected="selected">Alabama</option>
  <option value="AK">Alaska</option>
  ...
</select>

I'd like to select the second select since it has a value selected already with the select="selected"

like image 990
hybrid Avatar asked Dec 26 '22 09:12

hybrid


1 Answers

value is not an attribute of select tag.

So you need to try:

var emptySelect = $('select').filter(function() {
                     return $.trim( $(this).val() ) == '';
                  });
like image 116
thecodeparadox Avatar answered Feb 06 '23 09:02

thecodeparadox