How can I return null instead of a 'undefined' from a select element.
Basically I have 4 selects at the moment, but only the first one is populated with data at this point, but I want to grab the value from all of them when working with them.
<select class="changeValue" id="drpOne"></select>
<option id=1>1</option>
<option id=2>2</option>
<option id=3>3</option>
<select class="changeValue" id="drpTwo"></select>
JQuery:
$('.changeValue').change(function() {
var data = {};
data["Id1"] = $('#drpOne:selected').attr("id");
data["Id2"] = $('#drpTwo:selected').attr("id");
In this case, drpTwo will return 'undefined'. Is there anyway to get a null instead?
Use the or
operator ||
:
data["Id2"] = $('#drpTwo:selected').attr("id") || null;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With