How to disable removing selected option in Chosen? - jquery-chosen

I would like to prevent users to remove pre-selected options in my Chosen dropdown (multiple).
<select data-placeholder="Your Favorite Types of Bear" multiple class="chosen-select" tabindex="8">
<option value=""></option>
<option>American Black Bear</option>
<option>Asiatic Black Bear</option>
<option>Brown Bear</option>
<option>Giant Panda</option>
<option selected>Sloth Bear</option>
<option selected>Sun Bear</option>
<option selected>Polar Bear</option>
<option selected>Spectacled Bear</option>
</select>
So I want the Sloth Bear, Sun Bear, Polar Bear and Spectacled Bear selected, and the user can't remove it. How do I go about that?

I think there is no official way to do this.
But let me give you an idea.
Define your select items like this.
<select data-placeholder="Your Favorite Types of Bear" multiple class="my_select_box" tabindex="8">
<option value=""></option>
<option value="1">American Black Bear</option>
<option value="2">Asiatic Black Bear</option>
<option value="3">Brown Bear</option>
<option value="4">Giant Panda</option>
<option value="5" disabled selected>Sloth Bear</option>
<option value="6" disabled selected>Sun Bear</option>
<option value="7" disabled selected>Polar Bear</option>
<option value="8" disabled selected>Spectacled Bear</option>
</select>
Such that I appear like this,
So, now users cannot remove the selected options.
But when you are retrieving the selected values, make sure you are adding the selected values like this.
var selected = $(".my_select_box").val();
var alreadySelected = ['5', '6', '7', '8']
var totalSelected = selected.concat(alreadySelected);
Now, it's ready! You can play with it.

As of my knowledge, there is no perfect way for that. But as a workaround, you can try to disable the options you want to prevent so that they cannot modify their visibility.
You can try the below code:
<select data-placeholder="Your Favorite Types of Bear" multiple class="chosen-select" tabindex="8">
<option value=""></option>
<option>American Black Bear</option>
<option>Asiatic Black Bear</option>
<option>Brown Bear</option>
<option>Giant Panda</option>
<option disabled>Sloth Bear</option>
<option disabled>Sun Bear</option>
<option disabled>Polar Bear</option>
<option disabled>Spectacled Bear</option>
</select>

Related

testing library userEvent.selectOptions does not select option

I have a React component which renders a combobox (another external component) with an option. When deployed the component does work as expected.
<select class="country-select"
id="nationalityCode" name="nationalityCode" required="">
<option value="" disabled="" hidden="">Choose</option>
<optgroup label="Most used">
<option value="0052">Belgium</option>
<option value="0055">German Citizien</option>
<option value="0057">French</option>
<option value="0001">Dutch</option>
</optgroup>
<optgroup label="Alle Nationalities">
<option value="0052">Belgium</option>
<option value="0055">German Citizien</option>
<option value="0057">French</option>
<option value="0001">Dutch</option>
<option value="0050">Albanese</option>
<option value="0223">American Citizen</option>
</optgroup>
</select>
When using testing library userEvent.selectOptions, the select of an option is successfull. Only the expect fails with
Error: expect(received).toBe(expected) // Object.is equality
Expected: true
Received: false
When debugging the selection state for all options it shows that the initial selected option is selected.
if (changeValue.nationaliteit) {
const selectOption = screen.getAllByRole('option', { name: 'French' })[0];
await userEvent.selectOptions(
screen.getByRole('combobox', { name: 'Nationaliteit' }),
selectOption
);
expect(
(screen.getAllByRole('option', { name: 'French' })[0] as HTMLOptionElement).selected
).toBe(true);
}
Using
"#testing-library/user-event": "^14.4.3",
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^12.1.5",

how to forbid to screen reader read options length in select

CromeVox screen reader reads wrong options length for select.
Technically right, but not what I need.
<select name="month" id="month">
<option value="" disabled selected style="display: none;">Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
It reads "combobox two of thirteen"
How to tell screen reader to not read list length at all or force it read "combobox two of twelve"?
https://jsfiddle.net/mrs_kva/5wk6xjt9/
I cant remove empty option.
UPD:
on w3.org presented example have this issue too
see here.
Despite there are 12 fruits in select reader says "13"

Add optgroup dynamically to an existing select

I have an existing select with 5 options and a default blank value. I want to add an optgroup to the first 3 values and another optgroup to the last two options.
<select id="cityDropdown">
<option selected="selected" value="" label=" "> </option>
<option value="1000">Tokyo</option>
<option value="1001">Kyoto</option>
<option value="1002">Osaka</option>
<option value="1003">Incheon</option>
<option value="1004">Busan</option>
</select>
I want my select to look like this
<select id="cityDropdown">
<optgroup label="Japan">
<option value="1000">Tokyo</option>
<option value="1001">Kyoto</option>
<option value="1002">Osaka</option>
</optgroup>
<optgroup label="SoKorea">
<option value="1003">Incheon</option>
<option value="1004">Busan</option>
</optgroup>
</select>
Below is my existing code which no longer works since the values are no longer even.
$(document).ready(function() {
var select = $('#cityDropdown');
var opt1, opt2;
$('option', select).each(function(i) {
if (i % 2 === 0) {
opt1 = $(this);
} else {
opt2 = $(this);
var optgroup = $('<optgroup/>');
optgroup.attr('label', label);
opt2.add(opt1).wrapAll(optgroup);
}
});
});

using excel vba to change the value of a dropdown menu that after activate and populate a second dropdown menu

I am writin a macro excel to fill out a form in a web site.
My issue is that I can valorize the drop down menĂ¹ (is a select command) but this should activate and populate a second drop down menĂ¹.. that does not appear
The code is
' this is the first select to choice the REGION
<select name="ctl00$cp$ddlRegion" id="ctl00_cp_ddlRegion">
<option value="">seleziona...</option>
<option value="13">Abruzzo</option>
<option value="17">Basilicata</option>
<option value="18">Calabria</option>
<option value="15">Campania</option>
<option value="08">Emilia-Romagna</option>
<option value="06">Friuli-Venezia Giulia</option>
<option value="12">Lazio</option>
<option value="07">Liguria</option>
<option value="03">Lombardia</option>
</select>
'the second select, depending from the region above, valorize the CITY
<select name="ctl00$cp$ddlProvince" id="ctl00_cp_ddlProvince">
<option value="">seleziona...</option>
<option value="070">Campobasso</option>
<option value="094">Isernia</option>
</select>
MY CODE IS:
IE.document.ALL("ctl00$cp$ddlRegion").Value = "03" ' that is LOMBARDIA
but after this line the value it appear.. but the second select does not activate.
I have tried with
IE.document.ALL("ctl00$cp$ddlRegion").focus
IE.document.ALL("ctl00$cp$ddlRegion").click
IE.document.ALL("ctl00$cp$ddlRegion").fireEvent ("onchange")
IE.document.ALL("ctl00$cp$ddlProvince").focus
IE.document.ALL("ctl00$cp$ddlProvince").disabled = false
BUT NOTHING... does anybody knows why......
thank you in advance
Alex

jQuery addClass using input select

Im a bit of a jQuery novice.
I have 5 options in a drop down menu.
I want to add a class in a div depending on which option is selected.
This class will then change the layout of the content using CSS.
Here is an example if it helps!
Thanks
<form>
<select>
<option id="1">layout 1</option>
<option id="2">layout 2</option>
<option id="3" selected>layout 3</option>
<option id="4">layout 4</option>
<option id="5">layout 5</option>
</select>
<div class="3">styled content</div>
</form>
You can use the ".attr()" to set the class attribute of the div onchange.
You're best to change the option id to value first. then:
$("select").change(function() {
$("div").attr("class", $(this).val());
});
(EDIT) Change it to:
$("select#np_blog_layout").change(function() {
$("div#changebox").attr("class", $(this).val());
});
What 'rudeovski ze bear' said, but if you still want to set it to the div's class to the selected elements id, here it is.
$('select').change(function() {
$('div').attr('class', $(this).attr('id'));
});
First off, you don't have to use id to your options. Put the values in value attribute. Put ID to your div and select so you can select them using jQuery.
<form>
<select Id="selectElement">
<option value="1">layout 1</option>
<option value="2">layout 2</option>
<option value="3" selected>layout 3</option>
<option value="4">layout 4</option>
<option value="5">layout 5</option>
</select>
<div id="styledContent" class="3">styled content</div>
</form>
On JS
//Attach event handler to select element's onchange event
$('#SelectElement').bind("change",changeStyle);
//The event handler
function changeStyle()
{
//Set class to selected value
$('#styledContent').class($('#SelectElement').val());
}