33

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?

double-beep
  • 5,031
  • 17
  • 33
  • 41

3 Answers3

45

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  
}
4
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'; 
}
Harry
  • 87,580
  • 25
  • 202
  • 214
Abhidev
  • 7,063
  • 6
  • 21
  • 26
  • so for my select tag `var select1 = document.getElementById('1');` how to trigger when the option in select tag is clicked? should i use if else statement? – Francis Jessie S. Enriquez Jr. Oct 07 '13 at 10:39
  • @Abhidev: The JS code part was formatted as blockquote. I have changed it to code format now. Please feel free to roll it back if you have any concerns. – Harry Oct 07 '13 at 11:04
  • hey thanx @Harry...tried changing it to code format but stackoverflow said there are no changes in the code so it didn't allow. :P – Abhidev Oct 07 '13 at 11:15
0
document.getElementById("button").style.visibility="visible|hidden|collapse|inherit";
kondapaka
  • 132
  • 1
  • 10
  • 5
    It is always better to add an explanation to the answer mate. It will make it clearer for both OP and any future readers. Also, I think you are suggesting the use of one of those values and not all in a `|` separated format. – Harry Oct 07 '13 at 11:05
  • 1
    not op but visibilty:hidden still takes space on page unlinke display:none https://www.w3schools.com/cssref/playit.asp?filename=playcss_visibility&preval=hidden – GorvGoyl Aug 20 '20 at 09:26