I am sending the index number of dropdown's options that users selects from index. Now want to select that specific option as 'selected' on another view. How to make it using jQuery?
$('#book_selection').attr("disabled", "disabled");
$('#book_selection').selectedIndex = 1;
but its not working...
Use $('select[id="salesrep"]'). val() to retrieve the selected value. Use $('select[id="salesrep"]'). val("john smith") to select a value from dropdown.
Syntax of jQuery Select Option$("selector option: selected"); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $("selector option: selected").
STEP 1 − Create a select tag with multiple options and assign an id to the select tag. STEP 2 − Also, create an empty DOM with an id to display the output. STEP 3 − Let there be a button element for the user to click and see the option selected. STEP 4 − Let the user select an option from the dropdown list.
Use prop()
method, setting selectedIndex
to a jQuery object practically does nothing. As of jQuery 1.6 for modifying properties prop
method should be used instead of attr
method:
$('#book_selection').prop("disabled", true)
.prop('selectedIndex', 1);
Alternatives:
// Getting the DOM element object using bracket notation and `.get()` method
$('#book_selection')[0].selectedIndex = 1;
$('#book_selection').get(0).selectedIndex = 1;
Use .prop()
for setting the properties, By the way you are in need to set the value by using index, so in that case .eq(index)
will help you.
Try,
$('#book_selection option').eq(1).prop('selected',true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With