nc1991 nc1991 - 5 months ago 10
Javascript Question

Getting the values of a select using onchange function

I am trying to get a value of a select box, using onchange, but when I use

this.val


<select name="type" name="auctionlength" onchange="listing_type();" id="listingtype" class="form-control">
<option><?php echo System::translate("Choose an option"); ?></option>
<option value="auction"><?php echo System::translate("Auction"); ?></option>
<option value="buy"><?php echo System::translate("Buy It Now"); ?></option>
</select>


and the following is my function:

var listing_type = function()
{
console.log(this.val);
}


I have tried the following instead:

this.val();
this.value()
this.value
$(this).val()
$(this).value()


But still not working?

Answer

Like this:

HTML

<select name="type" name="auctionlength" onchange="listing_type(this.value);" id="listingtype" class="form-control">
   <option><?php echo System::translate("Choose an option"); ?></option>
   <option value="auction"><?php echo System::translate("Auction"); ?></option>
   <option value="buy"><?php echo System::translate("Buy It Now"); ?></option>
</select>

JS

function listing_type(value){
    console.log(value);
}
Comments