Dillinger
Dillinger

Reputation: 1903

How to check if a certain property is present in select

I've this html select:

<select id="select-filter-item" data-hasqtip="true" title="" aria-describedby="qtip-3">
        <optgroup label="Servizi" type="services-group">
              <option type="provider">Tutti i trattamenti</option>
              <option value="13" type="service">Taglio</option>
              <option value="14" type="service">Colore</option>
        </optgroup>
</select>

How you can see I've the first option without value property, I want check if the current value selected from the select have the property value or not, if yes get the value number as 13 or 14. How I can achieve this?

I get the value like:

var po = $('#select-filter-item').val();

Upvotes: 0

Views: 40

Answers (2)

charlietfl
charlietfl

Reputation: 171679

You can use isNaN() to see if the value is numeric or not

$('#select-filter-item').change(function(){
    var val = $(this).val();
    var isNumber = val !='' && !isNaN(val );
    alert(isNumber);
});

Upvotes: 2

steo
steo

Reputation: 4656

var selected = $("#select-filter-item option:selected"), 
    val = $(selected).val();

if(val){ //this checks if the value is not falsy (0, undefined, null, "", false)
   if(typeof val === "string"){
      //do something with your string
   } else if(typeof val === "number") {
      //do something with your number
   }
}


/* You can also use jQuery instead of css selector 
   to go through a div tree, as far as I know it has better
   performance 
*/

//var selected = $("#select-filter-item").find("option:selected"); 

Upvotes: 1

Related Questions