I have a div with a button at the end. I want that when someone presses that button another div (below the previous one) should appear with the content I have put it inside the div.
I am using the following code:
HTML:
<div>
<a id="button" href="#">REGISTER</a>
</div>
<br>
<br>
<div id="item">
<iframe src="some source" embedded=true" width="760" height="550" frameborder="0" marginheight="0" marginwidth="0">Loading</iframe>
</div>
and the jquery with script:
<script>
$(function() {
$( "#button" ).click(function() {
$( "#item" ).toggle();
});
});
</script>
The problem I am having is that the second div is appearing on page load itself initially and when I am clicking on the button its disappearing. What should I do?
The document. getElementById will select the div with given id. The style. display = "none" will make it disappear when clicked on div.
To show and hide div on mouse click using jQuery, use the toggle() method. On mouse click, the div is visible and on again clicking the div, it hides.
Create <img> element in the HTML code. Add style to <img> element and set display properties to none. Create a JavaScript “show()” function that can access the image and change the display property to block. Add button in HTML code which calls “show()” function when user clicks on it.
The hidden attribute hides the <div> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'.
HTML5 enables you to do it easily using the details tag.
<details>
<summary>Click to toggle division</summary>
<p>Here you can put some content...</p>
<p>... and anything else you want :)</p>
</details>
Here is the demo: https://jsfiddle.net/3wqyyf7m/
If you want an element to be hidden at first load, you can use the hidden
attribute;
<div id="item" hidden>...</div>
Demo: http://jsfiddle.net/xztwnp7f/1/
Read more at: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes#hidden
This is a relatively new attribute so if you need to support old browsers you can put this in your css to make it work;
*[hidden] { display: none; }
Source: http://davidwalsh.name/html5-hidden
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With