206

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/

3
  • do you want to set a particular option on load ? or your want to trigger your change event ? Commented May 11, 2012 at 8:02
  • Could you expand on the final solution. Are you trying to have an option forced when the page loads, or select a different option when something specific is selected? Commented May 11, 2012 at 8:02
  • hi, thanks for your feedback i was able to solve the problem and have documented it. Commented May 11, 2012 at 8:35

8 Answers 8

385

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');
Sign up to request clarification or add additional context in comments.

5 Comments

Also after changing option i wanted to execute the change event function. w.r.t this. alert box should execute.
@KishanThobhani you should call that after adding the handler. see the sample.
The important part of this solution is: The change event handler MUST be declared BEFORE the trigger (makes sense...), in my case that was the problem, thanks!
defined change function BEFORE you are using, solved my problem. Thanks.
because JS is a scripting language and scripting language has an interpreter, If you fire an event before binding in first place your result is not expected as you think. so let the interpreter bind every event then fire it. ;)
37

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.

1 Comment

Any difference between using .change() and .trigger('change'), or just syntax preference?
25

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"));

4 Comments

I tried all the above, but only this one works for me. I think, because my select was inside vue. Thanks @Mohamed23gharbi!
only this one works for me too.
Indeed, I didn't manage to send change event from a SELECT using jQuery event handler, while this worked flawlessly.
6 years after publishing this answer, I encountred same behavior, and the past me saved my day :D
18
$('#edit_user_details').find('select').trigger('change');

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

Comments

1

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>

Comments

1

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>

Comments

1

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.

Comments

0

You can try below code to solve your problem.

By default, first option select:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.