Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why JQuery hide() and show() does not work?

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?

like image 756
Luciano Avatar asked Jun 27 '13 16:06

Luciano


People also ask

How does jQuery show hide work?

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.

How do I toggle between show and hide in jQuery?

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.

Which jQuery function is a combination of hide () and show ()?

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.

What does show () do in jQuery?

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).


4 Answers

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

like image 53
Mohammad Adil Avatar answered Sep 27 '22 23:09

Mohammad Adil


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:

  • cache finding DOM elements beacuse they are expensive to find
  • use on instead of click, it works faster
  • $function() is an alias to document.ready, it's faster to write and less bytes to send over the network :)
  • you don't have to use div#id selectors, #id is enough because it should be unique in your page, moreover that way after jquery will use findElementById javascript function it will not have to perform additional check for div.
  • there is nice article about jQuery performance: artzstudio
  • input should not be split into open and close tag.

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');
    });
});
like image 22
0lukasz0 Avatar answered Sep 28 '22 01:09

0lukasz0


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

like image 42
Dhaval Marthak Avatar answered Sep 28 '22 00:09

Dhaval Marthak


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.

like image 35
PSL Avatar answered Sep 28 '22 01:09

PSL