0

I have a select dropdown which is dynamically populated and displays a list of servers.

I've currently got a script to display a padlock / open padlock to show if an option requires a password, an exert of this script shown below:

var has_password=true;
if(stations[i].Encryption =="none") has_password=false;
if(stations[i].Quality==100) img=
(has_password?"/images/pass.png":"/images/nopass.png");  

What I would like to do is hide the html password input field if the selected option doesn't require a password but I don't think my approach is correct:

if(stations[i].Encryption =="none");
$("#password-div").hide();
else
{
$("#password-div").show(); 
3
  • 3
    The hide() show() is correct, you need to attach an onchange event to your select so you can then do the hiding/showing of your div
    – Lixus
    May 18, 2017 at 14:34
  • 1
    Your approach will work given that you will attach the appropriate listeners. What is the problem you are facing right now?
    – orabis
    May 18, 2017 at 14:39
  • If you show us your html that would be useful also.
    – larz
    May 18, 2017 at 14:42

1 Answer 1

2

You can do something like this.. this is a demo I found on w3schools

function myFunction() {
    var x = document.getElementById("mySelect").value;
    document.getElementById("demo").innerHTML = "You selected: " + x;
}
<select id="mySelect" onchange="myFunction()">
  <option value="Audi">Audi
  <option value="BMW">BMW
  <option value="Mercedes">Mercedes
  <option value="Volvo">Volvo
</select>

<p>When you select a new car, a function is triggered which outputs the value of the selected car.</p>

<p id="demo"></p>

That should give you an idea for the onchange event. You have the hide() and show() correct. Just add the onchange event now.

2
  • I've been able to add the onchange event and get it to display the type of encryption in a label but not able to hide div yet, will keep trying. Thanks May 19, 2017 at 10:30
  • If you can create a dev of it I can take a look at it and help you. Use codepen.io or something like that. Otherwise show your new code you're working with and I'll take a crack at it. May 19, 2017 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.