Div to toggle it's display on/off but be able to click the input

javascript show/hide div onclick toggle
hide and show div using javascript with example
css show hide div on click
onclick show div and hide other div
onclick show hide div jquery demo
jquery toggle
javascript toggle
toggle css

I'm trying to make it so that whenever you click on either the button OR div itself, the display toggles. But whenever I click on the input inside of the div, the div disappears.

How can this be made so that you can still click on the input and the div not disappear? I've tried setting a z-index to the input but this fails.

Appreciate any help, thank you.

function doThis() {
  var el = document.querySelector('div');
  
  if (el.style.display === 'flex') {
      el.style.display = 'none';
      } else {
        el.style.display = 'flex';
      }
}
div {
  background: lightgreen;
  display: flex;
}
<button onclick='doThis()'>click me</button>

<div onclick='doThis()'>
  text <input type="text">
</div>

If you want the input click not to trigger the div click, you can use event.stopPropagation() function. It prevents event bubbling (passing the event to higher level DOM-elements).

function doThis() {
  var el = document.querySelector('div');

  if (el.style.display === 'flex') {
      el.style.display = 'none';
      } else {
        el.style.display = 'flex';
      }
}
div {
  background: lightgreen;
  display: flex;
}
<button onclick='doThis()'>click me</button>

<div onclick='doThis()'>
  text <input onclick='event.stopPropagation()' type="text">
</div>

jQuery toggle() Method, Tip: This method can also be used to toggle between custom functions. Syntax. $(​selector).toggle(speed,easing,callback). Parameter, Description. After you have saved your setting for the Toggle Module, duplicate the Divider Module you created and place it underneath the Toggle Module. This will frame the Toggle with a top and bottom divider line. After that, duplicate your Toggle Module and place it after the bottom divider line.

For a pure JavaScript solution (that doesn't need jQuery), see this answer from @Sabaz to How do I prevent a parent's onclick event from firing when a child anchor is clicked?:

document.getElementById("clickable").addEventListener("click", function( e ){
    e = window.event || e; 
    if(this === e.target) {
        // put your code here
    }
});

Your code wont be executed if clicked on parent's childs

.toggle(), Description: Display or hide the matched elements. version added: 1.0.toggle( [​duration ] [, complete ] ). duration (default:  To toggle a div visibility in jQuery, use the toggle() method. It checks the div element for visibility i.e. the show() method if div is hidden. And hide() id the div element is visible. This eventually creates a toggle effect. The toggle( speed, [callback]) method toggles displaying each of the set of matched elements using a graceful animation and firing an optional callback after completion.

you can do this:

function doThis(evt) { // <-- new: add argument
  evt.preventPropagation() // <-- new, works in all new browsers
  var el = document.querySelector('div');

  if (el.style.display === 'flex') {
      el.style.display = 'none';
      } else {
        el.style.display = 'flex';
      }
}

And add to your html:

onclick='doThis(event)'

How to Toggle a div's visibility by using a button click, In case you are interested in a jQuery soluton: This is the HTML <a id="button" href="#">Show/Hide</a> <div id="item">Item</div>. This is the  You can easily use show hide toggle div on click event in angular 6, angular 7, angular 8 and angular 9 application. In this example, i will give you two example of angular toggle element on click. one if using *ngIf and another is using [hidden]. we will create buttons with two methods.

Why cant you implement event stopPropagation for all input objects, Try ..

// select elements with js selectors and bind it
document.querySelector('input').onclick = function(e){
   e.stopPropagation()
};

and here is answer by Rex M

Toggle (Show/Hide) Element, <a href="#" onclick="toggle_visibility('foo');">Click here to toggle visibility of element #foo</a> <div id="foo">This is foo</div>. Our Learning  How do you hide a div but show another one, the solutions here are for hiding only one. Can someone please post code that can solve the real issue. I have multiple divs and I want to display one at a time as the rest are hidden. Simply put, I click another button and only the div linked to it is displayed while the others disappear

Just check the target element which is clicked

function doThis() {
 if(event.target.nodeName != "INPUT"){
 var el = document.querySelector('div');
   if (el.style.display === 'flex') {
     el.style.display = 'none';
   } else {
     el.style.display = 'flex';
   }
  }
}

Collapse · Bootstrap, Toggle the visibility of content across your project with a few classes and our with data-target </button> </p> <div class="collapse" id="collapseExample"> <div​  About three years late to the party, but I found this when I was looking to do the same thing, for a mobile menu, and subsequently found a very good solution, so I thought I'd post it for whoever else comes a-hunting.

Display only the clicked div and not all on toggle, with displaying a description div for a list item. When I click on a specific li (e.g. Work abo…), I'd like to toggle only the div for that particular li… Change the cell reference in the formula to another cell that doesn’t have a zero (0) or blank value. Enter #N/A in the cell that’s referenced as the divisor in the formula, which will change the formula result to #N/A to indicate the divisor value isn’t available.

Passing state from a toggle button to hide a Div, Hi folks, In a React project, I have a toggle button that shows and hides some info to the user. Now I want to connect a third part to the toggle. Login Form Signup Form Checkout Form Contact Form Social Login Form Register Form Form with Icons Newsletter Stacked Form Responsive Form Popup Form Inline Form Clear Input Field Hide Number Arrows Copy Text to Clipboard Animated Search Search Button Fullscreen Search Input Field in Navbar Login Form in Navbar Custom Checkbox/Radio Custom Select Toggle Switch Check Checkbox Detect Caps Lock Trigger Button on Enter Password Validation Toggle Password Visibility Multiple Step Form Autocomplete

Toggle, To apply this component, just add the uk-toggle="target: #ID" attribute to a <div class="uk-card uk-card-default" uk-toggle="cls: uk-card-primary; mode: media;  toggle definition: 1. a small bar of wood or plastic that is used to fasten something by being put through a hole or…. Learn more.

Comments
  • You can't using display:none as it hides the element and all it's contents. You might be able to do somthing with visibility though.
  • interesting. it works on this demo but not on my task. What does the (event) part mean? does it have to be called (event)?
  • event is a global that contains the last fired event on page (in case of onclick handler this will obviously be a click (also called MouseEvent in some browsers).