I learned that using a reference variable is faster than using $() every line of code (see my previous question): jQuery – Is it okay to use $('#ElementId') everytime?. Now my question is how can I use this reference variable to maximize the power of jQuery? Please see the example below:
Without reference variable:
var ValueOfSelected = $('#SelectElementId option:selected').val();
With reference variable (pseudo-code):
var SelectElement = $('#SelectElementId');
var ValueOfSelected = $(SelectElement).SelectedOption.val();
Note that SelectedOption.val() is the pseudo-code here. Is there such function anyway?
You can use
.find()to find the nestedoption.…but because it is a
selectelement, you can just use theval()[docs] method directly.This will give you the value of the selected
option.