Sunday 28 July 2019

javascript - onclick closing other onclicks



I have multiple div elements on my page, which have the display switch function .



 onclick = "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'" 
onclick="if(document.getElementById('spoiler2') .style.display=='none') {document.getElementById('spoiler2') .style.display=''}else{document.getElementById('spoiler2') .style.display='none'}"



I am looking for a way to close each other opened element when opening a new one, so that only one can stay open.



Thanks


Answer



It's pretty hard to understand without looking at your html, but you can try something like this:



$(document).ready(function() {

$("#light").on("click", function() {

$("#spoiler2").hide();
$(this).show();
});

});

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...