Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle visibility when user makes a selection

I have a select form with several options. I also have a div that I would like to show ONLY when a particular option is selected. Could you guys point me in the right direction? What would be the easiest thing to use for this?

Thank you :)

like image 927
bdeonovic Avatar asked Feb 04 '26 14:02

bdeonovic


2 Answers

Here is a simple and non-jQuery solution

window.onload=function() {
  var sel = document.getElementById("sel1");
  sel.onchange=function() {
     document.getElementById("otherDiv").style.display=(this.value=="other")?"":"none";
  }
  sel.onchange(); // set the visibility onload too in case of reload
}

<select id="sel1">
.
.
.
<option value="other">Other</option>
</select>
<div id="otherDiv">Other options</div>
like image 79
mplungjan Avatar answered Feb 06 '26 04:02

mplungjan


jQuery is a powerful javascript library that makes this kind of task trivial. Here is an example of what you're trying to do using it: jQuery - Using the radio button to show/hide a div.

like image 42
Bryan Agee Avatar answered Feb 06 '26 04:02

Bryan Agee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!