0

I have a simple function that shows a row of text on a button click. It works just fine, opening and closing with the button, but I'd like it so clicking anywhere on the page will toggle the box closed. Especially, since there are three other buttons in a vertical row and if the user clicks the second button, to open it's text row, it definitely needs to close the first.

src="https://code.jquery.com/jquery-3.5.0.js"></script>

<script>
    $( "#virtual-poss-btn" ).click(function() {
        $( "#virtual-poss-row" ).slideToggle( "slow" );
    });
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MC Squared
  • 43
  • 6
  • If one of the elements is clicked then call `slideToggle` on every other element. This will close all of them, then open the clicked one. You shouldn't invoke this behaviour if any part of the page is clicked. – Cjmarkham Feb 14 '22 at 19:11
  • This seems to work IF all of the other elements are already open. In my case, I have 4 buttons and 4 text elements that are all hidden. Each button opens it's associated text box and a 2nd click closes the box. If the user opens box 1 and does not close it, when he clicks to open box 2, box 1 should close. Only one box should be able to be opened at the same time. – MC Squared Feb 16 '22 at 15:06
  • Related: *[Suggested edits: do questions need both \[javascript\] and \[jquery\] tags?](https://meta.stackoverflow.com/questions/290244/suggested-edits-do-questions-need-both-javascript-and-jquery-tags)* – Peter Mortensen Jan 22 '23 at 18:06

0 Answers0