Avatar of Jaber Ahmad
Jaber AhmadFlag for Côte d'Ivoire

asked on 

Display values select

Good morning all !

I can't find a way to hide a value from a select except in this way:
SEL_Paiement.options[1].style.display = "block";
SEL_Paiement.options[2].style.display = "none";
SEL_Paiement.options[3].style.display = "block";
SEL_Paiement.options[4].style.display = "block";
SEL_Paiement.options[5].style.display = "block";
SEL_Paiement.options[6].style.display = "block";
SEL_Paiement.options[7].style.display = "block";

Open in new window

I wish I could do the same thing but with values and not options[1], like (I say a big mistake):
SEL_Paiement.value = "Hello".style.display = "block";

Open in new window


Can you help me please ?

cordially
JavaScript

Avatar of undefined
Last Comment
Michel Plungjan
ASKER CERTIFIED SOLUTION
Avatar of Jaber Ahmad
Jaber Ahmad
Flag of Côte d'Ivoire image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Michel Plungjan
You cannot reliably hide options. You can remove them

Some browsers will allow

SEL_Paiement.options[1].setAttribute('disabled', true);

Open in new window

and then have

select option[disabled] { display: none; }

Open in new window

in css

but the most cross browser safe version is to remove them and add them when needed:

window.addEventListener("load",function() {
  const allOpts = document.querySelectorAll("#SEL_Paiement options");
  document.getElementById("reset").addEventListener("click",function() {
    document.getElementById("SEL_Paiement").innerHTML=allOpts; 
  });
});

Open in new window

JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo