Javascript hide select option based on value

To show a hidden div when a select option is selected, you can set the value “style.display” to block.

Example

Following is the code −





Document










This is the Javascript

To run the above program, save the file name anyName.html(index.html) and right click on the file. Select the option “Open with live server” in VS Code editor.

Earn income with your HTML skills

Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest self-service freelancing marketplace for people like you.

Earn income with your HTML skills

Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest self-service freelancing marketplace for people like you.

This destructing can also be used inside of a loop, so you could then structure your provinces like so using array literals

const provinces = [
  ['AB', 'Alberta'],
  ['BC', 'British Columbia'],
  ['MB', 'Manitoba'],
  ['NB', 'New Brunswick'],
  ['NL', 'Newfoundland']
]

Here I am using a for…of loop to log out the alpha codes and province names.

for (const [alphaCode, province] of provinces) {
  console.log(alphaCode + ' - ' + province)
}

/* Outputs
AB - Alberta
BC - British Columbia
MB - Manitoba
NB - New Brunswick
NL - Newfoundland
*/

You can hopefully see that destructuring provides a clearer alternative to using indexes e.g. provincesArray[1][i] and

const [breakfast, lunch, dinner] = ['cornflakes', 'sandwich', 'bolognese']
console.log(breakfast) // 'cornflakes'
console.log(dinner) // 'bolognese'
0

In addition if you use ‘let’ or ‘const’ to define your variables you can take advantage of block scope

You could then remove these two lines

var newOption;
var newTextNode;

And define the variables inside of your loop instead. By using const here, we get an entirely new variable definition for each iteration of the loop.

How can I disable an option in a select based on its value in Javascript?

querySelector('option[value="'+optionValue. replace(/["\\]/g, '\\$&')+'"]'). disabled = true; The performance depends on the number of the options (the more the options, the slower the first one) and whether you can omit the escaping (the replace call) from the second one.

How do I hide selected option when item is already selected?

The first is using *ngIf and this is the preferred way. This removes the <option> tag from the DOM entirely. Alternatively, if you cannot remove the element from the DOM for some reason, you can also use CSS visibility: hidden .

How do you hide the Select option based on the option selected in the first selection?

User have to select the category first, than the sub category option will be displayed based on the first selection. If user pick category with value 3 it will display the sub category with option value of 1, 2 , 3 . Option value of 0 will be hide/ remove in the sub category .

How do I hide a select field?

The hidden attribute hides the <select> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <select> element is not visible, but it maintains its position on the page.