How can I change selected value of a DropDownList with having change method?

Sirwan Afifi

I was wondering if it's possible to get jQuery to select an option with having predefined change method.

Let me give me an example; I have a drop-down list like this:

<select id="ddl_BankRequestType">
    <option>-- Request Type ---</option>
    <option value="val1">Item1</option>
    <option value="val2">Item2</option>
</select>

If user selects Item1 or Item2 some logic will be executed:

$('#ddl_BankRequestType').change(function () {
     var selectedItem = $(this).find(":selected").text();
     if (selectedItem === 'Item1') {
         // codes
     } else if (selectedItem === 'Item2') {
         //other codes
     }
});

It works like a charm. Now in document.ready I want the second option be selected item, so I put this code:

$('#ddl_BankRequestType > option:eq(2)').attr('selected', true);

It also works, but I want the logic for Item2 to execute. I have tried the following, but they do nothing:

$('#ddl_BankRequestType > option:eq(2)').attr('selected', true).change();
$('#ddl_BankRequestType > option:eq(2)').attr('selected', true).trigger('change');
guvenckardas

You can check the code from DEMO

$(function (){
  $('#ddl_BankRequestType').change(function () {
      var selectedItem = $(this).find(":selected").text();

      console.log(selectedItem)
      if (selectedItem === 'Item1') {
        alert('item1');
      } else if (selectedItem === 'Item2') {
        alert('item2');
      }
  });

  $('#ddl_BankRequestType > option:eq(2)').attr('selected', true).change();        

});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I change the options from a dropdownlist based on the selected value from another dropdownlist using ajax?

How can I change the output value of an <option> if selected?

Change selected value of kendo ui dropdownlist

Change the URL id depend on the DropDownList selected value

How can I change the value inside the data method

How can I bind checkbox value and invoke a method on change?

How can I implement a method that change directly the value this with dart extension?

How can i change selected option background?

How can I populate a dropdownlist based on the selected value of another dropdownlist? MVC

How can i change Form value Action based on dropdown value selected

How can I change value from a node when having a server instance (node-opcua)

Programatically change value based on selected item from dropdownlist

How can I change the line width of all selected lines to the same value in Inkscape?

How can I change my current selected item by either the key or value of my datasource?

How can I change the value of a Django model field when a particular choice is selected?

PS: How can i change a value of an item of an array when selected with 'Where-Object'?

How can I change the color of a specific letter without having space

How can I change the format of a DateTime value?

How can I change the value of a submit button

How can I change a value of an NSObject at a keypath?

How can i change the value of $number here

How can i change a value in an generic arraylist?

How can I change button value with stylish?

how can i change value of transform with jQuery?

How can I change an array value with a click?

How can I change the stop loss value?

How can I change value array into array

How can I change a value inside of a setter?

How can I change the value of a row with indexing?