Hey, I wanted to make a JQuery show/hide. I basically have a select box with two options in it. I would like it so if a user selects a certain option out of the select box to show a div and if they choose the other one to not show it.
I would also like it if the box is hidden by default.
Something like this:
<select>
<option>Show Box If This is Clicked</option>
<option>Hide Box If This is Clicked</option>
</select>
If anyone can help it would be greatly appreciated. I have seen many tutorials on how to do this with a link but I dont know how to edit it so it works with a select box. Thanks!
Give the select an identifier, so you can target that element to attach an event on to it, and place a value on each option, which will be used to trigger a div with the same identifier.
Please call your select something other than mySelect. Choose a name that best describes the purpose of the selection.
<select id="mySelect">
<option value="showIfClicked">Show Box If This is Clicked</option>
<option>Hide Box If This is Clicked</option>
</select>
<div id="showIfClicked">
This content gets shown when the appropriate option is clicked.
</div>
And here’s the script. When the page loads it’ll set the select to unselected. This then ensures that the box is hidden by default.
Then it attaches a change event onto the select so that it hides all the appropriate boxes based on the option value, and then shows whichever one was chosen.
Finally it runs the event on the select box to activate the process as a part of the page load, which because nothing is selected hides all of the appropriate boxes.