Trigger change event <select> using jquery

Jquery

Jquery Problem Overview


is there anyway i could trigger a change event on select box on page load and select a particular option.

Also the function executed by adding the trigger after binding the function to the event.

I was trying to output something like this

<select class="check">
<option value="one">one</option>
<option value="two">two</option>
</select>

$(function(){
    $('.check').trigger('change'); //This event will fire the change event. 
    $('.check').change(function(){
      var data= $(this).val();
      alert(data);            
    });
});

http://jsfiddle.net/v7QWd/1/

Jquery Solutions


Solution 1 - Jquery

Use val() to change to the value (not the text) and trigger() to manually fire the event.

The change event handler must be declared before the trigger.

Here's a sample

$('.check').change(function(){
  var data= $(this).val();
  alert(data);            
});

$('.check')
    .val('two')
    .trigger('change');

Solution 2 - Jquery

To select an option, use .val('value-of-the-option') on the select element. To trigger the change element, use .change() or .trigger('change').

The problems in your code are the comma instead of the dot in $('.check'),trigger('change'); and the fact that you call it before binding the event handler.

Solution 3 - Jquery

$('#edit_user_details').find('select').trigger('change');

It would change the select html tag drop-down item with id="edit_user_details".

Solution 4 - Jquery

Another working solution for those who were blocked with jQuery trigger handler, that dosent fire on native events will be like below (100% working) :

var sortBySelect = document.querySelector("select.your-class"); 
sortBySelect.value = "new value"; 
sortBySelect.dispatchEvent(new Event("change"));

Solution 5 - Jquery

Give links in value of the option tag

<select size="1" name="links" onchange="window.location.href=this.value;">
   <option value="http://www.google.com">Google</option>
   <option value="http://www.yahoo.com">Yahoo</option>
</select>

Solution 6 - Jquery

If you want to do some checks then use this way

 <select size="1" name="links" onchange="functionToTriggerClick(this.value)">
    <option value="">Select a Search Engine</option>        
    <option value="http://www.google.com">Google</option>
    <option value="http://www.yahoo.com">Yahoo</option>
</select>

<script>
    
   function functionToTriggerClick(link) {
        
     if(link != ''){
        window.location.href=link;
        }
    }  

</script>

Solution 7 - Jquery

Based on Mohamed23gharbi's answer:

function change(selector, value) {
    var sortBySelect = document.querySelector(selector);
    sortBySelect.value = value;
    sortBySelect.dispatchEvent(new Event("change"));
}

function click(selector) {
    var sortBySelect = document.querySelector(selector);
    sortBySelect.dispatchEvent(new Event("click"));
}

function test() {
    change("select#MySelect", 19);
    click("button#MyButton");
    click("a#MyLink");
}

In my case, where the elements were created by vue, this is the only way that works.

Solution 8 - Jquery

You can try below code to solve your problem.

By default, first option select:

jQuery('.select').find('option')[0].selected=true;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkishanioView Question on Stackoverflow
Solution 1 - JqueryJosephView Answer on Stackoverflow
Solution 2 - JqueryThiefMasterView Answer on Stackoverflow
Solution 3 - JquerySohail xIN3NView Answer on Stackoverflow
Solution 4 - JqueryMohamed23gharbiView Answer on Stackoverflow
Solution 5 - JqueryAfraz AhmadView Answer on Stackoverflow
Solution 6 - JqueryAfraz AhmadView Answer on Stackoverflow
Solution 7 - JqueryTillitoView Answer on Stackoverflow
Solution 8 - JqueryKetan TambekarView Answer on Stackoverflow