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 7447655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:40:53+00:00 2026-05-29T12:40:53+00:00

tl;dr: How do I get a DOM element, that is made visible by a

  • 0

tl;dr: How do I get a DOM element, that is made visible by a click handler, to be the last element before close of body even if dynamically generated elements are being appended?

Essentially I want to have the click that opens the element also move it in the DOM to be last before body close even after some dynamic elements have been appended.


My issue stems from a modal that is being opened within another modal while there are multiple modals on one page. One catch is that the submodals exist in the DOM at page load while the primary modals are being instantiated onclick (the submodals use different code to generate while the primary ones use jQuery UI dialog).

If you open a modal and then open the submodal all is well. If you open a second modal then close it when you reopen that first modal the submodal will not appear as it is hidden below the second modal. If I append these submodals to the body, thus putting them after any generated modals they appear fine (but that creates other issues). In spite of what it looks like this does not appear to be a z-index problem (I’ve tried everything related to that with no luck), it appears to be related to where these modals are positioned in the DOM.

Code might be clearer:

This is what the html looks like on page load:

<div id='submodal_1' class='submodal'>foo</div>
<div id='submodal_2' class='submodal'>bar</div>
</body>

After you click to open the first primary modal window:

<div id='submodal_1'>subfoo</div>
<div id='submodal_2'>subbar</div>
<div id='primary_modal_1' class='ui-dialog' style='display:block'>foo</div>
</body>

After you click to open that modal’s submodal I have to move the submodal to be below the primary modal otherwise it will not appear (and again, z-index does not effect this blocking):

$('#submodal_1').insertAfter('#primary_modal_1');

At this point if you close that #primary_modal_1 it will be hidden but still there in the DOM. If you open a second the code will now look like:

<div id='submodal_2' class='submodal'>subbar</div>
<div id='primary_modal_1' class='ui-dialog' style='display:none'>foo</div>
<div id='submodal_1' class='submodal'>subfoo</div>
<div id='primary_modal_2' class='ui-dialog' style='display:block'>bar</div>
</body>

At this point if you reopen the first primary modal and try to open its submodal the submodal will not appear. However if you move the submodal to be below that last primary modal, like so:

<div id='submodal_2' class='submodal'>subbar</div>
<div id='primary_modal_1' class='ui-dialog' style='display:block'>foo</div>
<div id='primary_modal_2' class='ui-dialog' style='display:none'>bar</div>
<div id='submodal_1' class='submodal'>subfoo</div> //<-- moved this line
</body>

The submodal will apear correctly. I want to ensure that the submodals are always last in the DOM, regardless of how many primary modals are appended.

  • 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-29T12:40:56+00:00Added an answer on May 29, 2026 at 12:40 pm

    The append method of JQuery “Insert content, specified by the parameter, to the end of each element in the set of matched elements.”

    $('body').append(...);
    

    You’ll have to move the element each time something is added dynamically.

    Check this JSFiddle:
    http://jsfiddle.net/eZ2Dq/

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

Sidebar

Related Questions

.get() converts a jQuery object to a DOM element that Javascript can use without
I have a <video> element that is generated by js, and I need to
So i have one div element that i append on my DOM with a
So I'm having a cross-browser JavaScript issue. I've got a <select> DOM element that
I want to get one element that is already on the page in the
I have a dom element that contains a fully qualified name as part of
Possible Duplicate: How to get a DOM Element from a JQuery Selector I can
I'm trying to find a way to get the DOM element of a marker.
How can i get the DOM element which fired onclick, in this situation :
I have a javascript function that receives a dom element as a parameter. In

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.