I have a div called ‘info’ that I want to be displayed when a button is clicked.
<button>Toggle</button>
<div id="toggle">
Test
</div>
Basically I would like to use jQuery to reveal it with a sliding feature but I don’t know how. I can find most of the code online and see snippets, but I don’t actually know WHERE to put most of it, could someone please walk me through what and where I would need to put the relevant bits of code (from scratch) to get this working? Thank you.
CSS
jQuery
A little explanation:
$('button')is the selector to thebuttontag.$('button').on('click',is for bind click event tobutton.the second parameter of
on()here is callback handler and code within it will execute after click to thebutton.$('#toggle')is the selector to thedivwithid=toggle.$('#toggle').slideToggle()will make a sliding (up-down) effect to thedivwhen button click.Note
You should do your code within jQuery dom ready function. Which is like:
in short
As these are JavaScript code, so you need to put them within
<script>tag. for example:But, if you write this code to an external file then you have to include that file within
<head>tag like below:According to comment
Is it possible to make the button disappear when it is first clicked?
Yes, possible. Try like below: