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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:22:11+00:00 2026-06-04T10:22:11+00:00

I made a modal plugin but for some reason one of the divs that

  • 0

I made a modal plugin but for some reason one of the divs that I generate isn’t getting a click event listener that I am attaching to it.

<a class="close" id="js-close-modal"></a> is what I’m referring to. I’m using jQuery’s on, but that doesn’t seem to help.

        var $caller = $(this);
        var $modal = $('.modal');

        $caller.on('click', $caller, function(e) {
            e.stopPropagation();

            $('#js-close-modal').on('click', $('#js-close-modal'), function(e) {
                $('.modal_outer').remove();
                $('#modal_page_cover').remove();
            });


            var modal_outer = $('<div />');
            modal_outer.addClass('modal_outer');
            modal_outer.css({
                top: ($(window).scrollTop()) + 'px'
            });

            var $div = $('<div />');

            var modal = $('<div />');
            modal.addClass('modal');

            var modal_inner = $('<div />');
            modal_inner.addClass('modal_inner');

            modal.append(modal_inner);
            modal_outer.append(modal);

            var body = $('body');
            body.append(modal_outer).hide().fadeIn(100);
            modal_inner.text('yo');

            var close = $('<a />');
            close.addClass('close').attr('id', 'js-close-modal');
            close.insertBefore(modal_inner);


            var page_cover = $('<div />');
            page_cover.attr('id', 'modal_page_cover');
            body.prepend(page_cover);

});

Demo: JSFiddle

Any idea why?

  • 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-06-04T10:22:14+00:00Added an answer on June 4, 2026 at 10:22 am

    You’re passing a jQuery object in as your selector on the $.on method. You should instead be passing in a selector alone:

    $("body").on("click", "#js-close-modal", function(){
      /* Do stuff */
    });
    

    You made the same error on $caller.on(). You don’t bind $.on() to the element you’re clicking, you bind it to one of the parents of that element. I’ve bound the even to the body element, but you should ideally bind it to a much closer parent.

    Making the change I demonstrated above fixes your close button: http://jsfiddle.net/P5B4q/22/

    Of course you’re creating the close button in the same code, so event delegation isn’t really necessary. You could do away with the code above, and modify your close button upon creation:

    var close = $('<a />');
    close.addClass('close').attr('id', 'js-close-modal');
    close.insertBefore(modal_inner);
    

    Replace that with:

    $("<a>", { class: "close", id: "js-close-modal" })
      .on("click", function(){ $('#modal_page_cover, #modal_outer').remove() })
      .insertBefore( modal_inner );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering if any of you can help me: I've made a signup modal that
Made a custom obj called Item with some string fields and one float. .h
I have a table that I've made sortable using the jQuery plugin Tablesorter 2.0
I Created a modal dialog box with jquery and made a little tweak that
I have a fairly complex model for one of my pages; it's made of
I use Acceleo in order to generate code with a model I have made.
I made a dll in c# but i need to export the functions. Is
I made an iphone app to capture image from camera and to set that
I have a custom WordPress plugin I made and it doesn't seem to work
I've made an ASP.NET web form that uses the standard ASP.NET validation. I'd like

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.