How to hide a div on second click with JavaScript

hide and show div using javascript with example
javascript show/hide div on click
onclick show div and hide other div
css show hide div on click
javascript show/hide div onclick toggle
javascript hide element by id
javascript hide element by class
hide div when click outside jquery

I have this little script which shows my div on the first click, but I want it to hide if it is clicked again!

Do I have my if...else statement wrong?

<a onClick='view()'>OTHER HTML CONTENT</a>
<div id="a" class="superHide"><img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/iagLogo_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/TelstraLogo_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/FootballFedAus_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/pngLng_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/NRMA_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/BupaLogo_300px.jpg" alt="" width="150" height="125" /></div>

<script>
function view() {
var testMe=document.getElementById('a').getAttribute('display');
  if(testMe=='block'){
        document.getElementById('a').setAttribute('style', 'display:none');
  } else{
        document.getElementById('a').setAttribute('style', 'display:block');
  }
}
</script>

.superHide {
    display:none;
}

This should work, I copied your HTML and applied the JS to do as you need. You'll notice the onClick='view(this, "a")' provides the element to the function

<a onClick='view(this, "a")'>OTHER HTML CONTENT</a>
<div id="a" class="superHide"><img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/iagLogo_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/TelstraLogo_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/FootballFedAus_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/pngLng_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/NRMA_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/BupaLogo_300px.jpg" alt="" width="150" height="125" /></div>


function view(item, targetId) {
    if(item.dataset.clickCount == undefined) { item.dataset.clickCount = 0 }
    item.dataset.clickCount = Number(item.dataset.clickCount) + 1

    let displayFormat = ['block', 'none'][item.dataset.clickCount % 2]
    let displayStyle = `display:${displayFormat}`

    document.getElementById(targetId).setAttribute('style', displayStyle);
}

</script>

I dig your raw flavour so I continued using the builtin functionality.

The trick here is the modulo (modulus) flip on the counter setup within the element. First I applied a counter to the dataset on the element, Then increment clickCount on every click. The dataset naturally stores as a String type, so I convert it to make sure the math increments.

Then using the modulo % 2, the click count flips between 0 and 1. This allows a cleaner string and saves repetition.

For one last trick, provide the target node ID from the click item. Now this function will on any clicky item against any target without code changes.

Hope it helps.

How to hide a div when the user clicks outside of it using jQuery , How to hide a div when the user clicks outside of it using jQuery? A mouseup event is to first checked upon the document. filter_none. $(document).mouseup( function (e) { The closest() method is called on the target click. This method returns the first ancestor of the selected element in the DOM tree. The element is This tutorial will show you how to create a hidden Div and display it with the click of a link. There are a few reasons you may want to hide a Div in a website design. You may want to create a drop down type menu or a box that will show more information when you click a link. Another reason would be for SEO purposes.


When getting attributes, you want to reference the style property on the DOM element directly rather that try to use the getter. I believe the getter only works if the style is set inline directly on the HTML. If the style is set through CSS, it will be contained only on that prop. Your getter was always returning null. Since null == 'block' returns false, the display block was always ran. Something like this will work:

function view() {
  var target = document.getElementById('a');
  var shown = target.style.display === 'block;
  target.setAttribute('style', `display:${shown ? 'none' : 'block'}`);
}

WebD2: Using Javascript to Hide and Show Content, So we'll have to change the style dynamically, using JavaScript, triggered by an onclick event. First, add the element that you want the user to click: <button onclick� HTML and JavaScript. <!--. Show / Hide Div on Button Click using JavaScript.--> < html > < head > < title > Show / Hide Div on Button Click using JavaScript.</ title > < script type ="text/javascript"> function showHideDiv ( ele) { var srcElement = document. getElementById ( ele); if ( srcElement != null) { if ( srcElement. style. display == "block") { srcElement. style. display = 'none'; } else { srcElement. style. display = 'block'; } return false; } } </ script > </ head > < body


Your Image is not visible i.e: Image you used is not correct.

So please use correct Image and use below script to get your result.

It seems your image is not being shown here.

so Even according your question below script will work fine.

But the problem is actually different. Your images are not being loaded.

I have attached the reason why your images are not being shown in here.

function view() {
var testMe=document.getElementById('a').style.display;
console.log(testMe);
  if(testMe=='none' || testMe == ''){
        document.getElementById('a').style.display = 'block';
  } else{
        document.getElementById('a').style.display = 'none';
  }
}
.superHide {
    display:none;
}
<a onClick='view()'>OTHER HTML CONTENT</a>
<div id="a" class="superHide"><img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/iagLogo_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/TelstraLogo_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/FootballFedAus_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/pngLng_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/NRMA_300px.jpg" alt="" width="150" height="125" />
<img class="alignnone size-full wp-image-940 imgWrap" src="http://boxingclever.net.au/wp-content/uploads/2019/10/BupaLogo_300px.jpg" alt="" width="150" height="125" /></div>

How To Toggle Between Hiding And Showing an Element, Toggle between hiding and showing an element with JavaScript. Toggle Hide and Show. Click the button! Toggle (Hide/Show) an Element. Step 1) Add HTML� Hide and show a div with JavaScript In this second example, we hide and show a div using a JavaScript library, called jQuery. This time, we won’t hide the div when it’s hovered but when it’s clicked. By default the divs will be hidden; once they are clicked, they become visible, displaying the text inside.


Hide and show a div, I will also show you another version of hiding and showing divs using jQuery. In this second example, we hide and show a div using a JavaScript library, called if I click on the first div to see its content, the other divs should remain hidden. 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


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 visualizza(idDiv) { … the second time I click on the same div table hidden div gets visible as� 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 and XML. w3schools .com THE WORLD'S LARGEST WEB DEVELOPER SITE


Toggle (Show/Hide) Element, Inline usage: Click here to toggle visibility of element #foo This is foo. React to CSS, from Vue to D3, and beyond with Node.js and Full Stack. Sometimes you are not able to use JavaScript but you want to show and hide some div. It is possible, and very easy! You must create 3 elements: input – checkbox with id, eg. “trigger” label – we treat it as toggle button; box – simple div; Input checkbox must be placed just before the box. Label can be placed where you want.