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

  • Home
  • SEARCH
  • 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 4081882
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:07:05+00:00 2026-05-20T18:07:05+00:00

When we run following script under Firefox … var d = $(<div class=’test’></div>); d.hide();

  • 0

When we run following script under Firefox …

var d = $("<div class='test'></div>");
d.hide();
$("body").prepend(d);
d.show();

… and look to HTML, this inserted element will have style attribute:

style=”display: block;”

Under Webkit, element will have:

style=”display: none;”

This scenario in used in a JavaScript component, i develop. This component has a collection of HTML elements in it’s internal variables, and inserts they to specified destination container.

Because inserted element having display-property initialized in style attribute, it overrides CSS. This breaks the layout of a page.

As a quick solution, i can store “style” attribute, before element is inserted to the DOM, and, after inserting, write stored version over created one.

Is there any better solution ?

Why this ever happens and how can i check, whether element is not yet inserted to the DOM ?

  • 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-20T18:07:06+00:00Added an answer on May 20, 2026 at 6:07 pm

    When I do that with either Chrome or Safari (both WebKit-based browsers), if I inspect the element with the built-in tools, it has no style.display property at all, and so the default div style of display: block is used. (Here’s a version with some text in the div, so it’s easier to see and to find with the DOM inspector.)

    So I suspect the problem lies elsewhere. For instance, is there intervening code that may be failing on WebKit, so that d.show(); never ends up getting called? That would certainly explain it. Easy enough with the built-in tools in Chrome or Safari to set a breakpoint on the code creating the div and walk through it.

    Re your question:

    …how can i check, whether element is not yet inserted to the DOM ?

    That question was asked here on StackOverflow just recently, and one of the answers specific to jQuery was rather elegant:

    if (d.closest("body").length == 0) {
        // It's not in the DOM yet
    }
    

    Update: Re your comment below

    Look at this test page with Firefox. The div has “style=display: block;” explicitly defined. Under Webkit, it has empty style attr. I’m using built-in inspector in both Firefox and Safari.

    Ah, okay, so the problem isn’t a display: none in WebKit browsers (your statement about that in the question led me astray), but rather that Firefox (and possibly other Gecko browsers) end up having display: block on the element.

    I’d probably approach this like this:

    var d = $("<div class='test'></div>");
    d.addClass("hidden");
    $("body").prepend(d);
    d.removeClass("hidden");
    

    …with this CSS:

    .hidden {
        display: none;
    }
    

    Live copy

    That way, you know you won’t end up with a style.display property set at all.


    Update 2: The other thing you can do is remove the style.display property directly:

    var d = $("<div class='test'>Hi there</div>");
    d.hide();
    $("body").prepend(d);
    d.show();
    d[0].style.display = "";
    

    Live example

    You alluded to effects, so if you’re doing fadeIn or something like that, use the callback:

    var d = $("<div class='test'>Hi there</div>");
    d.hide();
    $("body").prepend(d);
    d.fadeIn(function() {
        this.style.display = "";
    });
    

    Live example

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

Sidebar

Related Questions

No related questions found

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.