How to return 'null' instead of 'undefined' from a selected item

How can I return null instead of 'undefined' from the selected item.

In principle, I have 4 choices at the moment, but only the first one is filled with data at this point, but I want to get a 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'. Anyway, to get zero?

+4
source share
1 answer

Use the or || operator :

 data["Id2"] = $('#drpTwo:selected').attr("id") || null; 
+8
source

Source: https://habr.com/ru/post/1304442/


All Articles