0
<!DOCTYPE html> 
<html> 

<head> 
   <title> 
       Hide or show HTML elements using 
       visibility property in JavaScript 
   </title> 

   <style> 
           .container { 
           height: 80px; 
           width: 250px; 
           border: 2px solid black; 
           background-color: green; 
           color: white; 
   </style> 
</head> 

<body> 
   <center> 
       <div class="container"> 
           <h1>Number of Child</h1> 
       </div> 

       <p class="container2"> 
           Click the buttons to show 
           or hide the green box 
       </p> 
 <input type="radio" id="female" name="gender" value="female"  onclick="showElement()"
        checked>
           <label for="female">Female</label>
        <label for="male">Male</label>
         <input type="radio" id="male" name="gender" value="male" onclick="hideElement()">  

   </center> 

   <script type="text/javascript"> 
       function showElement() { 
           element = document.querySelector(".container"); 
           element = document.querySelector(".container2"); 

           element.style.visibility = 'visible'; 
       } 

       function hideElement() { 
           element = document.querySelector(".container"); 
           element = document.querySelector(".container2"); 

           element.style.visibility = 'hidden'; 
       } 
   </script> 
</body> 

</html>

Okay so here's the simple code, I want to target 2 elements, which are : ".container"and ".container2"

but the method document.querySelector only choose one, how can I fix this ?

Link to the code : https://jsfiddle.net/7cgmq0tx/

2
  • 2
    querySelectorAll()? Commented Jan 25, 2020 at 23:49
  • I've already try it, didn't worked out Commented Jan 25, 2020 at 23:52

2 Answers 2

1

Second querySelector overrides first one, you should use two variables:

   <script type="text/javascript"> 
       function showElement() { 
           element1 = document.querySelector(".container"); 
           element2 = document.querySelector(".container2"); 

           element1.style.visibility = 'visible'; 
           element2.style.visibility = 'visible'; 
       } 

       function hideElement() { 
           element1 = document.querySelector(".container"); 
           element2 = document.querySelector(".container2"); 

           element1.style.visibility = 'hidden'; 
           element2.style.visibility = 'visible'; 
       } 
   </script>
Sign up to request clarification or add additional context in comments.

1 Comment

Oh yeah indeed, the solution was basic, thank you !
0

Because you are trying to make element equal something twice in the same block, javascript will instinctively make only the last one specified be true when the code is run. Which is why it only affects one element when you click the buttons. Try this instead:

function showElement() { 
           element = document.querySelector(".container"); 
           element2 = document.querySelector(".container2"); 

           element.style.visibility = 'visible';
           element2.style.visibility = 'visible';
       } 

       function hideElement() { 
           element = document.querySelector(".container"); 
           element2 = document.querySelector(".container2"); 

           element.style.visibility = 'hidden';
           element.style.visibility = 'hidden'; 
       }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.