Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the JavaScript equivalent of jQuery's hide() and show()? [duplicate]

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?

like image 492
Francis Jessie S. Enriquez Jr. Avatar asked Oct 07 '13 10:10

Francis Jessie S. Enriquez Jr.


People also ask

Is jQuery hide () is equivalent to?

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).

What is show hide in JavaScript?

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").

How do you use hidden in JavaScript?

style. visibility = "hidden"; document. getElementById(id). style.

What is opposite of hide in JavaScript?

The opposite of visibility: hidden is visibility: visible .


2 Answers

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   } 
like image 156
Vinay Pratap Singh Bhadauria Avatar answered Sep 20 '22 14:09

Vinay Pratap Singh Bhadauria


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';  } 
like image 31
Abhidev Avatar answered Sep 22 '22 14:09

Abhidev