Using jQuery, how can I get the selected item's value in a select when the page loads? I can do it onChange etc but cant figure out the way on page load.
Here's my code:
<select name="symb" id="symb">
<option value="1#10#6">Basic Personal</option>
<option selected="selected" value="2#20#6">Basic Family</option>
<option value="7#90#12">Advanced Personal</option>
<option value="8#120#12">Advanced Family</option>
<option value="9#130#12">Optimum Personal</option>
<option value="10#170#12">Optimum Family</option>
</select>
This is the script I'm using for getting the option value onchange:
$(document).ready(function() {
$("#symb").change(function() {
var val = $(this).symbCost(0);
})
$.fn.symbCost = function(pos) {
var total = parseInt($("option:selected").val().split('#')[pos]);
return total;
}
});
We can select text or we can also find the position of a text in a drop down list using option:selected attribute or by using val() method in jQuery. By using val() method : The val() method is an inbuilt method in jQuery which is used to return or set the value of attributes for the selected elements.
Method 1: Using the value property: The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.
Approach 1: First select the options using jQuery selectors then use prop() method to get access to its properties. If the property is selected then return the default value by using defaultSelected property.
With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.
$(document).ready(function() {
alert($("#symb").val());
});
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