1

JSFiddle Demo of My Code

I need to change the selected OPTION in a dropdown & Also Trigger the onChange of the dropdown.

$('#type').val('option 1');
$('#type').val('option1');

I tried the above to select

<option value="option1">option 1</option>

However no luck so far!

2
  • can you explain more what you want ? here in your example not able to understand what you want to do ? Commented Apr 8, 2015 at 4:18
  • In the JSFiddle Demo I put my code, When pressing CHANGE it is expected to update the value in the dropdown however it is currently not updating. Commented Apr 8, 2015 at 4:19

4 Answers 4

1

Use the following:

$('#type').val('option1').change();

You should pass the value of the value attribute to the .val() function.

You can trigger the change event by calling .change() (with no arguments) on the jQuery object that represents the <select> element.

Note: Calling .change() with no arguments is a shortcut for calling .trigger('change').

jsfiddle

Sign up to request clarification or add additional context in comments.

2 Comments

How can I also add the following within the option: selected="selected"
Setting the value with .val() should cause the <option> with that value to have its selected property set.
0

you can use your code like this. #openvz is not present in your html code that's why its not working.and you need to use option1 instead option 1

$('#change').click(function(){
        $('#type').val('option1');
    });

js fiddle

Comments

0

You need to cancel the click and use the correct ID

$('#change').on("click",function(e){
    e.preventDefault();// cancel
    $('#type').val('option1').change(); // must be the value, not the text
});

Also you cannot submit a select. If you want, you can submit the form if it does not have a field with name="submit"

You will need to tell us what you want to have happen when you change the select too. for example change the select and have it submit the form it is in

$(function() {
  $('#change').on("click",function(e){
    e.preventDefault();// cancel
    $('#type').val('option1').change(); // assuming you want to trigger the change 
  });
  $('#type').on('change',function() {
    $(this).closest("form").submit();
  });
});

Comments

0

try this Demo

<option value="option 1">option 1</option>

$('#change').click(function(){
    $('#type').val('option 1');
});

1 Comment

No, now it is identical to @Ris' solution which is not adding anything to this question either

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.