Change the selected value of a drop-down list with jQuery
jQuery's documentation states:
[jQuery.val] checks, or selects, all the radio buttons, checkboxes, and select options that match the set of values.
This behavior is in jQuery
versions 1.2
and above.
You most likely want this:
$("._statusDDL").val('2');
Add .change()
to see the option in the dropdown list frontend:
$("._statusDDL").val('2').change();
How to set the 'selected option' of a select dropdown list with jquery
Try this :
$('select[name^="salesrep"] option[value="Bruce Jones"]').attr("selected","selected");
Just replace option[value="Bruce Jones"]
by option[value=result[0]]
And before selecting a new option, you might want to "unselect" the previous :
$('select[name^="salesrep"] option:selected').attr("selected",null);
You may want to read this too : jQuery get specific option tag text
Edit: Using jQuery Mobile, this link may provide a good solution : jquery mobile - set select/option values
Jquery : Change Dropdown value on change event
$('.first-select').on('change', function() {
if(this.value == 'grade') {
$('.second-select option').show();
$('.second-select').prop('selectedIndex',0);
}
else if(this.value == 'year') {
$('.second-select option[value="4"]').hide();
$('.second-select option[value="3"]').hide();
$('.second-select').prop('selectedIndex',2);
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<select class="form-control first-select" id="class" name="class">
<option value="grade">Grade</option>
<option value="year">Year</option>
</select>
</div>
<div class="form-group">
<select class="form-control second-select" id="class_year" name="class_year" >
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option
</select>
</div>
How to get jQuery dropdown value onchange event
Try like this
$("#drop").change(function () {
var end = this.value;
var firstDropVal = $('#pick').val();
});
How can I set the value of a DropDownList using jQuery?
$("#mydropdownlist").val("thevalue");
just make sure the value in the options tags matches the value in the val method.
changing a drop down list's selected value with jquery
The line test.children("#room option[value='3']").attr('selected', 'selected')
has the basic idea, but children
gets the children elements only and filters them. So it would get #room
, but it wouldn't get the option
elements because the <option>
s are grandchildren of test
. Instead, use find
, which searches through all descendant elements. Here's the code:test.find("#room option[value='3']").attr('selected', 'selected')
jQuery: set selected value of dropdown list?
You need to select jQuery in the dropdown on the left and you have a syntax error because the $(document).ready
should end with });
not )};
Check this link.
How to Change the selected value of a drop down list according to value in a text box in jQuery
Why loop through the list. Just try to set it directly. If there is no match, it will not change...
$("#dropFinish2").val($('#f2').val());
Here is a working example
if value is all in lowercase you can do...
$("#dropFinish2").val($('#f2').val().toLowerCase());
or if value casing cannot be changed...
$("#f2").keyup(function(){
var input = $(this).val().toLowerCase();
$('#dropFinish2 option').each(function () {
if(input == this.value.toLowerCase()){
$("#dropFinish2").val(this.value);
}
});
});
Case-insensitive working example
Related Topics
JavaScript Object Literal: What Exactly Is {A, B, C}
How to Pass Parameters Rendered from Backend to Angular2 Bootstrap Method
JavaScript Implementation of Gzip
"Syntaxerror: Unexpected Token ≪ in Json At Position 0"
No Visible Cause For "Unexpected Token Illegal"
Group Array of Object Nesting Some of the Keys With Specific Names
How to Connect to SQL Server Database from JavaScript in the Browser
Can't Perform a React State Update on an Unmounted Component
Preloading Images With JavaScript
How to Read an External Local Json File in JavaScript
Creating Object With Dynamic Keys