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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:54:46+00:00 2026-06-01T01:54:46+00:00

I can’t find the problem. Why is this code making many copies of the

  • 0

I can’t find the problem. Why is this code making many copies of the “X”-button when I close the red-layer and when I click the first button again?

I thought the init worked like a proper constructor that just runs one time, right?

Here is a jsfiddle illustrating the problem:

http://jsfiddle.net/yoniGeek/mWghr/

Thanks for your time!

Y/

the html:

<div class="Main">
     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
         Phasellus non nisl mauris. Phasellus eget viverra mi. 
         Curabitur elementum tristique nibh, et faucibus eros 
         fermentum ut. Pellentesque non nisi augue. 
         Nulla facilisis ultrices malesuada. Sed bibendum lacus 
         sed lorem auctor rutrum. 
         iam semper justo id diam
     </p>
     <p id="last_child" > Lorem ipsum dolor 
         sit amet, consectetur adipiscing elit. 
         Phasellus non nisl mauris. Phasellus eget viverra mi. 
         Curabitut interdum velit ultricies. 
     </p>
     <div id="layer_on" ></div>
 </div>

the jquery-code:

(function(){

    $('html').addClass('js');

    //var red_layer;
    var red_layer = {
        red_box: $('#layer_on'),

        init: function() {
            $('<button></button>', {
                text: 'push me'
            }).insertAfter('.Main #last_child').on('click', this.show);
        },

        show: function() {
            red_layer.close.call(red_layer.red_box);
            red_layer.red_box.show();
        },

        close: function() {
            var $this = $(this);
            $('<span class="close">X</span>').prependTo(this).on('click', function() {
                $this.hide();
            });
        }
        //anonymous function

    }; // red_layer object ends

    red_layer.init(); //We call it back (the function )

})();
  • 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-01T01:54:47+00:00Added an answer on June 1, 2026 at 1:54 am

    Why don’t you add the close button in the init method? You only need to add the button once. As it is, you are adding the X button every time you show the box. Every time you click push me, you call show(), which in turn calls .close(), which prepends another X to the box. Instead, call close from your init function:

    var red_layer = {
        red_box: $('#layer_on'),
    
        init: function() {
            // Console.log(this);
            $('<button></button>', {
    
                text: 'push me'
    
            }).insertAfter('.Main #last_child')
                    .on('click', this.show);
    
            red_layer.close.call(red_layer.red_box);  // <--- HERE
        },
    
        show: function() {
            // NOT HERE
    
            red_layer.red_box.show();
        },
    

    Demo: http://jsfiddle.net/xwmVr/

    I would argue that your close method doesn’t need to be a method. It simply adds the X button, which seems like a one-time thing. It also is not obvious what it is doing. It seems like calling close would actually close the box, but it doesn’t in this case.

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

Sidebar

Related Questions

can someone explain why the compiler accepts only this code template<typename L, size_t offset,
Can anyone help me trying to find out why this doesn't work. The brushes
Can we close all known/unknown connections to database with the code? I'm using Access
can any one tell me how can change this java code into objective c.is
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can I run this in a Windows command prompt like I can run it
Can anybody help me? What should be the datatype for this type -07:00:00 of
Can't figure out how to do this in a pretty way : I have
Can i get the source code for a WAMP stack installer somewhere? Any help
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.