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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:10:11+00:00 2026-05-13T17:10:11+00:00

The following code is fairly self explanatory, but I am running in to an

  • 0

The following code is fairly self explanatory, but I am running in to an issue binding the click event to an element that has been created.

You can see at line 25 I am creating a div with id of ‘overlay’. I then set it’s css properties.

Then at line 65 I bind click to trigger the hiding of the modal. The problem is, it doesn’t work. If I put the div in the html, the the .click works as expected.

Any help is appreciated.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Modal</title>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript">

$(document).ready(function() {  

    // Select the link(s) with name=modal
    $('a[name=modal]').click(function(e) {

        // Cancel the link behavior
        e.preventDefault();

        // Get the id of this link's associated content box.
        var id = $(this).attr('href');

        // Find the screen height and width
        var overlayHeight = $(document).height();
        var overlayWidth = $(window).width();

        // Create the overlay
        $('body').append('<div id="overlay"></div>');

        //Set css properties for newly created #overlay
        $('#overlay').css({
                'width' : overlayWidth,
                'height' : overlayHeight,
                'position':'absolute',
                'left' : '0',
                'top' : '0',
                'z-index' : '9000',
                'background-color' : '#000',
                'display' : 'none'          
            });

        // Get the viewpane height and width
        var winH = $(window).height();
        var winW = $(window).width();

        // Center the modal
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        // Transition effects for overlay
        $('#overlay').fadeIn(1000).fadeTo(1000,0.8);

        // Transition effect for modal
        $(id).fadeIn(1000); 

    });

    // Close link click = trigger out
    $('.modal .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();

        $('#overlay').fadeOut(1000);
        $('.modal').fadeOut(200);
    });     

    // Overlay click = trigger out
    $('#overlay').click(function () {
        $('#overlay').fadeOut(1000);
        $('.modal').fadeOut(200);
    });

    // Load rules in to modal
    $('#rules .text').load('rules.html');

});

</script>
<style type="text/css">

.modal{
  position:absolute;
  display:none;
  z-index:9999;
}
#rules{
  width:600px; 
  height:400px;
}
#rules p{
    background: #fff;
    margin: 0;
    padding: 0;
}
#rules .head{
    background: #ddd;
    text-align: right;
    padding: 0 10px;
    height: 30px;
}
#rules .text{
    height: 370px;
    padding: 0 20px;
    overflow: auto;
}

</style>
</head>
<body>

<p><a href="#rules" name="modal">Open Modal</a></p>

<div id="rules" class="modal">
    <p class="head"><a href="#" class="close">close</a></p>
    <p class="text"></p>
</div>

</body>
</html>
  • 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-13T17:10:12+00:00Added an answer on May 13, 2026 at 5:10 pm

    The click event to the overlay is binded before the element exists. You need to use live binding to retain the binding – otherwise you’d have to do the bind every time you create the element. Just change your function to use live() like this:

    $('#overlay').live('click', function () {
        $('#overlay').fadeOut(1000);
        $('.modal').fadeOut(200);
    });
    

    The click event for .modal .close works as it is already defined in the DOM when the event is bound.

    Normal event binding only considers the elements that currently exist in the DOM while events bound with live() work also on all future elements that match the selector.

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

Sidebar

Related Questions

I'm just learning about python. I'm fairly new. I have the following code that
Per the code below, I am getting the following message. I am fairly certain
I am fairly new to jQuery and have written the following code to show
I came accross the following code today and I didn't like it. It's fairly
jQTouch sometimes fires my click event two times which result in another element (often
This seems to be something that have been discussed by many. But unfortunately, I
I'm fairly new to using coffeescript and I have the following code eventBus =
I'm fairly new to mysql and I was wondering if the following code should
Having some problems with a solution that apparently works: <script type=text/javascript > //following code
I have a fairly simple question that has me stymied. I am trying to

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.