I have a simple hide and show jQuery code and I want to ask if there is equivalent of this to JavaScript? Here's my code.
$(document).ready(function() { $("#myButton").hide(); $("#1").click(function() { $("#myButton").show(); $("#myButton").click(function() { $("#myButton").hide(); }); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select> <option id="1">Science</option> </select> <input type="button" value="Click" id="myButton" />
I have followed some codes from the comments below but they are not working:
<script> document.getElementById('myButton').style.display = 'none'; function selectOptionsupdated(select) { document.getElementById('myButton').style.display = 'block'; } </script> <select onSelect="selectOptionsupdated(this)"> <option id="1">Science</option> </select> <input type="button" value="Click" id="myButton" />
What I want is at first the button is hidden, and when I click the <option>
tag "Science" the button appears and when I click the button, the button is hidden after it is being clicked. And what if there are more <option>
tags?
jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page).
Style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”. document. getElementById("element").
style. visibility = "hidden"; document. getElementById(id). style.
The opposite of visibility: hidden is visibility: visible .
this is simple
document.getElementById('myElement').style.display = 'block'; // show document.getElementById('myElement').style.display = 'none'; // hide
add a onSelect="selectOptionsupdated(this)
in your select
then
function selectOptionsupdated(select){ //do your stuff here }
var myButton = document.getElementById('myButton'); //hide myButton.style.display = 'none'; //show myButton.style.display = 'block';
Update for your select tag..try this
html
<select id="list"> <option id="1">Science</option> </select>
js
var list = document.getElementById('select'); list.addEventListener('change', listSelect, false); function listSelect(){ var selected = list.options[list.selectedIndex].value;//Selected option value //hide myButton.style.display = 'none'; //show myButton.style.display = 'block'; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With