I have a simple DIV and can't get it to hide() and show().
I guess I am doing it right but can not find what is wrong with it.
<div id="thediv" >hola</div>
<input type="button" value="click to show">This is the div content</input>
$(document).ready(function() {
$("div#thediv").hide();
alert($("div#thediv").hide().attr("id"));
});
$("button").click( function() {
$("div#thediv").show();
alert('click');
});
Also, I created a fiddle at link"http://jsfiddle.net/rt9Fc/".
Any ideas?
You can show and hide HTML elements using the jQuery show() and hide() methods. The hide() method simply sets the inline style display: none for the selected elements.
The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.
Answer: Use the jQuery change() method The following example will demonstrate you how to show and hide div elements based on the dropdown selection or selected option in a select box using the jQuery change() method in combination with the show() and hide() methods.
jQuery Effect show() Method The show() method shows the hidden, selected elements. Note: show() works on elements hidden with jQuery methods and display:none in CSS (but not visibility:hidden).
put your click handler inside document.ready and change your selector to $("input:button")
-
$(document).ready(function () {
$("div#thediv").hide();
alert($("div#thediv").hide().attr("id"));
$("input:button").click(function () {
$("div#thediv").show();
alert('click');
});
});
Demo --->
JsFiddle
There is more proper version of your code: JsFiddle
HTML:
<div id="thediv">hola</div>
<input type="button" value="click to show"/>
JavaScript:
$(function() {
var $myDiv = $("#thediv");
$myDiv.hide();
alert($myDiv.attr("id"));
$("input[type=button]").on('click', function() {
$myDiv.show();
alert('click');
});
});
Some useful notes:
$function()
is an alias to document.ready, it's faster to write and less bytes to send over the network :)Probably you wanted to have this:
HTML:
<div id="thediv">
hola
<input type="button" value="click to show"/>
</div>
That way we can optimise JavaScript:
$(function() {
var $myDiv = $("#thediv");
$myDiv.hide();
alert($myDiv.attr("id"));
$myDiv.find("input[type=button]").on('click', function() {
$myDiv.show();
alert('click');
});
});
Change button selector
: as you were using simple <input type='button'/>
still if you want to use $('button')
change your markup to <button></button>
$("#thediv").hide();
alert($("div#thediv").hide().attr("id"));
$("input[type='button']").click( function() {
$("#thediv").show();
});
DEMO -->
JsFiddle
Change your button selector to :button
or use input. button
selector is used for <button>Somebutton</button>
$(document).ready(function() {
var $thediv = $('#thediv').hide(); //Cache the object here. Also you can shain it through
$(":button").click( function() {
$thediv.show();
alert('click');
});
});
Fiddle
If you have id, don't prefix it with tagname. it will make the selector slower. So just use #thediv
instead of div#thediv
. Also try to cache the jquery object to a variable if you are using it in multiple places, this will avoid calling the jquery object creation everythime.
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