I got a div
element, so when I click on it, another div
which is hidden by default is sliding and showing, here is the code for the animation itself:
$('#myelement').click(function(){
$('#another-element').show("slide", { direction: "right" }, 1000);
});
How can I make that so when I click on the #myelement
one more time (when the element is showed already) it will hide the #another-element
like this:
$('#another-element').hide("slide", { direction: "right" }, 1000);
?
So basicly, it should work exactly like a slideToggle
but with the show/hide
functions. Is that possible?
This question is related to
javascript
toggle
jquery
$(document).ready( function(){
$("button").click(function(){
$("p").toggle(1000,'linear');
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js</script> <script>
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
</head>
<body>
<p>Welcome !!!</p>
<button>Toggle between hide() and show()</button>
</body>
</html>
You can use this code for toggle your element var ele = jQuery("yourelementid"); ele.slideToggle('slow'); this will work for you :)
Make use of jquery toggle function which do the task for you
.toggle() - Display or hide the matched elements.
$('#myelement').click(function(){
$('#another-element').toggle('slow');
});
this will work for u
$("#button-name").click(function(){
$('#toggle-id').slideToggle('slow');
});
You can use .toggle()
function instead of .click()
....
Source: Stackoverflow.com