Show/hide div with javascript

javascript show/hide div onclick toggle
show/hide div with animation javascript
css show/hide div on click
onclick show hide div jquery demo
javascript hide element by class
onclick show div and hide other div
jquery show hide toggle
how to check div is display:none or block in javascript

I have a menu which should show the sub-elements organised in a div when clicked.

I provide below the structure of the html

function showOrHide() {
  if (window.innerWidth < 800) {

    var div = document.getElementById("links");
    if (div.style.display == "block") {
      div.style.display = "none";
    } else {
      div.style.display = "block";
    }
  }
}
<li>
  <div id="image" onclick="showOrHide()">
    <span>Test1</span>
  </div>
  <div id="links">
    <span>Test2</span>
  </div>
</li>
<li>
  <div id="image" onclick=" showOrHide()">
    <span>Test3</span>
  </div>
  <div id="links">
    <span>Test4</span>
  </div>
</li>

Element IDs should be unique within the entire document.

You must try creating unique id per div.

function showOrHide(rowNum) 
{ if (window.innerWidth < 800) { 

    var div = document.getElementById("links_"+ rowNum);
    if (div.style.display == "block") 
    {
        div.style.display = "none";
    }
    else 
    {
        div.style.display = "block";
    }
}
}
<li>
<div id="image_1" onclick="showOrHide(1)">
<span>Test1
</span></div>
<div id="links_1">
<span >Test2</span>
</div></li>
<li>
<div id="image_2" onclick=" showOrHide(2)">
<span>Test3
</span></div>
<div id="links_2">
<span >Test4</span>
</div></li>

jQuery Effects - Hide and Show, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  In order to show or hide an element, manipulate the element's style property. In most cases, you probably just want to change the element's display property: element.style.display = 'none'; // Hide element.style.display = 'block'; // Show element.style.display = 'inline'; // Show element.style.display = 'inline-block'; // Show


Change id and structure accordingly.

<li>
   <div id="image_1" onclick="showOrHide(1)">
      <span>Test1
      </span>
   </div>
   <div id="links_1">
      <span >Test2</span>
   </div>
</li>
<li>
   <div id="image_2" onclick=" showOrHide(2)">
      <span>Test3
      </span>
   </div>
   <div id="links_2">
      <span >Test4</span>
   </div>
</li>

JavaScript:

function showOrHide(rowNumber) 
{ if (window.innerWidth < 800) { 

    var div = document.getElementById("links_"+ rowNumber);
    if (div.style.display == "block") 
    {
        div.style.display = "none";
    }
    else 
    {
        div.style.display = "block";
    }
                               }
}

Show/hide 'div' using JavaScript, How to show or hide an element: In order to show or hide an element, manipulate the element's style property. In most cases, you probably just want to change  How TO - Toggle Hide and Show Step 1) Add HTML: Step 2) Add JavaScript: W3.CSS Tutorial


Just provide a parameter to your showOrHide function which will be the selector for your div and do the toggling to that selector. See the implementation below

<li>
    <div id="image" onclick="showOrHide('#links_1')">
        <span>Test1</span>
    </div>
    <div id="links_1">
        <span >Test2</span>
    </div> 
</li>
<li>
    <div id="image" onclick=" showOrHide('#links_2')">
        <span>Test3</span>
    </div>
    <div id="links_2">
        <span >Test4</span>
    </div>
</li>

And the JS:

function showOrHide(divSelector) 
{ 
    var div = document.querySelector(divSelector);
    if (div.style.display == "none") 
    {
        div.style.display = "block";
    }
    else 
    {
        div.style.display = "none";
    }
}

Hide or show elements in HTML using display property , 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 Create some div and assign them an id or class and then add styling to it. filter_none. edit close. Using javascript onclick () with two buttons ('show' and 'hide', each in their own DIVs), it would work something like this: By default, the 'show' button is hidden, the main DIV and hide button are displayed Clicking the 'hide' button hides main DIV and 'hide' button itself, displays the 'show' button


var elems = document.getElementsByClassName("links");
Array.from(elems).forEach(v => v.addEventListener('click', function() {
  this.parentElement.getElementsByClassName('links')[0].classList.toggle('hidden');
}));
.hidden {
  display: none;
}
<li>
  <div class="links">
    <span>Test1 </span>
  </div>
  <div class="links">
    <span>Test2</span>
  </div>
</li>
<li>
  <div class="links">
    <span>Test3
</span></div>
  <div class="links">
    <span>Test4</span>
  </div>
</li>

How to show and hide elements with vanilla JavaScript, Showing and hiding elements with vanilla JS is pretty straightforward. Adding little bit harder. Today, let's look at how to write simple show/hide methods. <div class="toggle-content is-visible">Visible by default.</div> <div  In jQuery, you can use the.toggle () method to toggle an element, any element. However, if you are looking for a pure JavaScript solution, then here it is. I am sharing a simple code here that shows how easily you can toggle or show/hide a DIV element using JavaScript.


Your main issue here is that an element's id must be unique to that element. Other elements cannot have the same id. Instead, you can pass through the element into your showOrHide() method using this, and then get the next element using nextElementSibling. This will work provided your HTML structure is consistent:

function showOrHide(elem) {
  if (window.innerWidth < 800) {
    const div = elem.nextElementSibling;
    if (!div.style.display || div.style.display == "block") {
      div.style.display = "none";
    } else {
      div.style.display = "block";
    }
  }
}
<li>
  <div id="image" onclick="showOrHide(this)">
    <span>Test1</span>
  </div>
  <div class="links">
    <span>Test2</span>
  </div>
</li>
<li>
  <div id="image" onclick=" showOrHide(this)">
    <span>Test3</span>
  </div>
  <div class="links">
    <span>Test4</span>
  </div>
</li>

Show or hide a DIV - JavaScript, Im trying to hide and show after clicking on the visible part of a div table. I wrote this function: <script type="text/javascript"> function  Right-click on the project and select Add -> Web Form and name it as Home.aspx. Now paste the below JavaScript code in the head section of the Home.aspx page. JavaScript. <script>. function showhide () {. var div = document.getElementById ("newpost"); if (div.style.display !== "none") {.


Hide and Show DIV using JavaScript, Now paste the below JavaScript code in the head section of the Home.aspx page​. JavaScript. <script>; function showhide(); {; var div  Show Hide Div Generator. Toggle button show hide div generator,radio button show hide div, checkbox show hide div JavaScript <script> Highlight. Preview Edit Code


How to Show/Hide or Toggle a DIV element using JavaScript, However, if you are looking for a pure JavaScript solution, then here it is. I am sharing a simple code here that shows how easily you can toggle or show/hide a DIV  Here Mudassar Ahmed Khan has explained how to show and hide HTML DIV with TextBox when CheckBox is checked (selected) and unchecked (unselected) using JavaScript and jQuery. jQuery Cheat Sheet will help you create the code for animations, various effects and other features for your website.


Javascript show hide div onclick toggle, The toggle() method simply toggles the visibility of elements. The matched elements will be revealed or hidden immediately, with no animation,  $(selector).hide(speed,callback); $( selector ).show( speed,callback ); The optional speed parameter specifies the speed of the hiding/showing, and can take the following values: "slow", "fast", or milliseconds.