Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the value of drop down list on page load with jQuery

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;
    }
});
like image 462
bikey77 Avatar asked Jun 27 '12 08:06

bikey77


People also ask

How we can get value of dropdown in jQuery?

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.

How do I capture a dropdown value?

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.

How can get default selected value of dropdown in jQuery?

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.

How do I get the selected value of multiselect dropdown in jQuery?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.


1 Answers

$(document).ready(function() {
    alert($("#symb").val());
});
like image 180
slash197 Avatar answered Nov 15 '22 06:11

slash197