I am trying to write a jQuery function to hide/show a div on a click.
I have an <a>
tag with the class my-button
. A div with the class my-div
is initially hidden with display: none
when the page loads. When a user click on my-button
and my-div
is hidden, I want to show my-div
. When my-div
is visible, I want to be able to hide it when a user click anywhere on the page other than on my-div
. This also includes a click on my-button
.
<a class="my-button">Click me!</a>
<div class="my-div">Show Me or Hide Me!</div>
I tried to use the .toggle()
function without any success; my-div
won't show after a click on my-button
. If I replace .toggle()
by .show()
, my-div
will show and hide if I click anywhere on the page but not if I click again on my-button
.
$(document).mouseup(function(e) {
var $container = $(".my-div");
$(".my-button").click(function(){
$container.toggle();
});
if (!$container.is(e.target) && $container.has(e.target).length === 0) {
$container.hide();
}
});
What would be the best way to achieve what I want.
Is this what you're looking for?
$(document).mouseup(function(e) {
var $container = $(".my-div");
if (!$container.is(e.target) && !$(".my-button").is(e.target) && $container.has(e.target).length === 0) {
$container.hide();
}
});
$(".my-button").click(function() {
$(".my-div").toggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="my-button">Click me!</button>
<div class="my-div">Show Me or Hide Me!</div>
You must properly attached the event to the button and use toggle()
to hide or show the div
$(document).ready(function(e) {
$(".my-button").click(function(e){
$(".my-div").toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a class="my-button">Click me!</a>
<div class="my-div">Show Me or Hide Me!</div>
First I would try show/hiding just on the button click Give this a try. First of all give your button an id="show_hide" and your div an id="my_div"
$('#show_hide').click(function (e) {
var x = document.getElementById('my_div');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
});
You can change the ID's to whatever you want, but I suggest this in case you want to give other elements the same class but without this functionality
Alternatively you could add this to make it disappear when clicking anywhere else other than the button you should be able to use the following function
$(document).ready(function(){
$(document).mousedown(function(e){
var x = document.getElementById('my_div');
if( e.button == 1 ) {
var isHovered = $('#my_div').is(":hover");
if (!isHovered) {
x.style.display = 'none';
}
}
});
});
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
Hi there is it possible to redirect to another page using ajax? I have this piece of code that I have been working on to try this
I am working on a WordPress website, with WooCommerce functionality
I've been fascinate with Jigsaw when I been got with it's pageNow, I wanna create a sliding content that smoothly appear when I click on a certain link but I don't know how to do it using CSS and HTML
I have the code below to fetch iTunes charts directly from the RSS and display itHowever, I only want to display the informations on the rss entry for one specific id