Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7168881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:52:18+00:00 2026-05-28T14:52:18+00:00

I have a bit of an issue with a toggle visibility function which operates

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:52:19+00:00Added an answer on May 28, 2026 at 2:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bit of an issue here. As Im new to jQuery this
I have a bit of an issue here. I have a Python script which
I have a bit of an issue for my JSP site. I have it
I have a bit of an issue with a site I maintain. I have
hello fellow java developers. I'm having a bit of an issue here. I have
I have an issue that is driving me a bit nuts: Using a UserProfileManager
I have this bit of javascript written with jQuery 1.2.5. It's contained inside the
This is an odd problem, so I have to provide a bit of background.
So I have a bit of an issue. I work for a small startup
I have a bit of an odd issue. I have a projects controller where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.