How to return 'null' instead of 'undefined' from select element

Posted by MrW on Stack Overflow See other posts from Stack Overflow or by MrW
Published on 2010-03-18T09:58:30Z Indexed on 2010/03/18 10:01 UTC
Read the original article Hit count: 315

Filed under:

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?

© Stack Overflow or respective owner

Related posts about jQuery