This question already has answers here:
How to select more than one option in multiple selection list using jquery?
(4 answers)
Closed 2 years ago.
<select>
<option value="0"></option>
<option value="1"></option>
<option value="2"></option>
<option value="3"></option>
</select>
Is there a way to select that options where have this values: 0, 2, 3
Something like this:
$('option[value=0,2,3]')
No, you can't put a list in an attribute selector.
You can use a list of selectors separated by comma, but there's no shorthand.
$('option[value=0],option[value=2],option[value=3]')
Perhaps a better solution would be to give all those options the same class, then use a class selector.
use the :not() css function option:not([value=1])
const selected = $('option:not([value=1])').text('selected')
console.log(selected)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select>
<option value="0"></option>
<option value="1"></option>
<option value="2"></option>
<option value="3"></option>
</select>
Related
This question already has answers here:
How can i get the unselected items from dropdown using jQuery? [closed]
(1 answer)
Get list of values from dropdown that are NOT selected using jquery
(2 answers)
How to get currently unselected option value when un selecting from multi list box
(2 answers)
get unselected list using jquery multi select box
(2 answers)
Closed 3 years ago.
By default, I have all the dropdown items selected. But when a user unselects one of them, I want to get the value of that unselected option.
I have tried the following option:
This is my html code:
<select class="ss-select" data-dropup-auto="false" id="ss_options" multiple="multiple" name="ss">
<option selected="selected" value="1">One</option>
<option selected="selected" value="2">Two</option>
<option selected="selected" value="3">Three</option>
<option selected="selected" value="4">Four</option>
<option selected="selected" value="5">Five</option>
</select>
This is the jquery code I tried:
$("#ss_options").change(function(){
alert($(this).val());
});
With this code, when I unselect an option (example: one), I get the values as [2, 3, 4, 5]. I am aware that I can do array subtraction to get the desired value, but is there some better of doing it?
Use :not and :selected:
alert($("#ss_options option:not(:selected)").val());
This question already has answers here:
How to display only the text in datalist HTML5 and not the value?
(3 answers)
Closed 5 years ago.
I want to hide datalist value. Am using this code
<input list="options" oninput="console.log(this.value);" />
<datalist id="options">
<option value="1">Product 1</option>
<option value="2">Product 2</option>
<option value="3">Product 3</option>
</datalist>
I mean in the inputbox this showing values and product 1
i need to show only product 1,2,&3...
hide value (1,2,3)
.
Note: Here am passing value 1,2,3 so i just want to hide that only
here my code https://jsfiddle.net/69u5Leoa/
You may want to use select, because for the select element, the user is required to select one of the options you've given.
For the datalist element, it is suggested that the user select one of the options you've given, but he can actually enter anything he wants in the input.
you can see this to get more information HTML Form: Select-Option vs Datalist-Option
<select id="options">
<option></option>
<option value="1">Product 1</option>
<option value="2">Product 2</option>
<option value="3">Product 3</option>
</select>
Value="<%# DataBinder.Eval(Container, "ItemIndex") %>" it can solution this problem
This question already has answers here:
Changing the selected option of an HTML Select element
(14 answers)
Closed 7 years ago.
<select name="cat1" class="form-control input-lg">
<option value="">All Businesses</option>
<option value="78">Deals</option>
<option value="46">Delivery</option>
<option value="45">Dispensary</option>
<option value="47">Doctor</option>
</select>
I know you could simply add the selected="selected" to the whatever option to make it default in the drop down. Currently the "All Businesses" option is the default one showing. I want the Dispensary option to show as default
<select name="cat1" class="form-control input-lg">
<option value="">All Businesses</option>
<option selected="selected" value="45">Dispensary</option>
<option value="46">Delivery</option>
<option value="47">Doctor</option>
<option value="78">Deals</option>
</select>
so basically I want that achieved but I am unable to edit the HTML code. Is there a way I can achieve it without touching the HTML?
$('select[name="cat1"] > option[value="45"]').attr('selected','selected');
This jQuery code will select that option element and add that attribute to it.
This question already has answers here:
Get clicked option in multiple dropdown
(4 answers)
Closed 8 years ago.
I have a select multiple list:
<select id="List" multiple="multiple">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
I trigger the event on the select:
$("select#List").change(function() {
// do ajax stuff...
});
I need to get the value of the clicked option that fired the change event.
Example:
if the option 1 is already selected, and I select the "option 2" that triggers the .change() event, I want get the value of "option 2".
(I've already read this post Get clicked option in multiple dropdown, but it doesn't work for me).
What does not work for you, I've checked the answers in link which you posted and it works.
See working example on fiddle: http://jsfiddle.net/unkxt8h3/
The only thing here is, that the event is fired even when you deselect the option. But checking if it's selected or not should be easy peacy ;)
html:
<select id="List" multiple="multiple">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
JS:
$("#List option").click(function () {
var value = $(this).attr("value");
console.log(value);
alert(value);
});
This question already has answers here:
How can I set the value of a DropDownList using jQuery?
(17 answers)
Closed 8 years ago.
Hi I know this was asked many times, I'm looking for a simple solution.
<select onchange="this.options[this.selectedIndex].value">
<option>--Select--</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
When a user select an option let's say One the select box would display the numerical 1.
Thanks in advance
Why don't you just use a numeral for the display option on each?
e.g. <option value="1">1</option>
(and take the inline javascript out entirely).
example: http://jsfiddle.net/79xvY/
This will set the actual innerHTML value of the select box, if that is what your goal is
<select onchange="this.options[this.selectedIndex].innerHTML=this.value">
<option>--Select--</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>