Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does jquery's show/hide function work?

I have a bit of an issue with a toggle visibility function which operates on the hidden attribute of an element. Trouble is, this lacks browser compatibility..

function hide(e) {$(e).hidden=true;}    
function show(e) {$(e).hidden=false;}

Googling this issue I came across the method of toggling the style.display property, like so..

function toggle(e) {
document.getElementById(e).style.display = (document.getElementById(e).style.display == "none") ? "block" : "none";
}

..but this seems sub-optimal, because you can't have a generic show/hide function that sets the display property to block. What if the element in question sometimes is supposed to have a inline or something?

How does for example jQuery solve this issue?

like image 347
jenswirf Avatar asked Jan 23 '12 10:01

jenswirf


People also ask

How does hide work in jQuery?

The jQuery hide() method is used for the purpose of hiding specified elements. Once the elements are hidden they will completely disappear from the sight of the user. You can pass three parameters to the hide() function which are; speed, easing, and callback. All of these parameters are optional.

What is show hide in JavaScript?

Style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”. document. getElementById("element"). style.

How do you show hide in CSS?

You can hide an element in CSS using the CSS properties display: none or visibility: hidden . display: none removes the entire element from the page and mat affect the layout of the page. visibility: hidden hides the element while keeping the space the same.

What does show () do in JavaScript?

Definition and Usage 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). Tip: To hide elements, look at the hide() method.


1 Answers

It stores the old display value in a data attribute called olddisplay and then uses the value of that to restore it when showing the element again. See the implementation here. You can check the implementation of any jQuery method on that site.

In the following code snippets I've annotated the important line with a //LOOK HERE comment.

The important part of the show method:

for (i = 0; i < j; i++) {
    elem = this[i];

    if (elem.style) {
        display = elem.style.display;

        if (display === "" || display === "none") {
            elem.style.display = jQuery._data(elem, "olddisplay") || ""; //LOOK HERE
        }
    }
}

When hiding an element it firstly stores the current display value in a data attribute:

for (var i = 0, j = this.length; i < j; i++) {
    if (this[i].style) {
        var display = jQuery.css(this[i], "display");

        if (display !== "none" && !jQuery._data(this[i], "olddisplay")) {
            jQuery._data(this[i], "olddisplay", display); //LOOK HERE
        }
    }
}

And then simply sets the display property to none. The important part:

for (i = 0; i < j; i++) {
    if (this[i].style) {
        this[i].style.display = "none"; //LOOK HERE
    }
}

Note

The above code is taken from jQuery version 1.6.2 and is obviously subject to change in later versions.

like image 128
James Allardice Avatar answered Oct 06 '22 23:10

James Allardice