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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:08:37+00:00 2026-05-30T01:08:37+00:00

<div id=wrapper> <button id=ppp>button</button> <div id=content style=display:none;></div> </div>​ if i click the button,the content

  • 0
<div id="wrapper">
    <button id="ppp">button</button>
    <div id="content" style="display:none;"></div>
</div>​

if i click the button,the content will be show(open),

if the content is on show(open),when i click document(except the content),the content will be close.

var $ = function (id){
return !id ? null : document.getElementById(id);
}

var addEventSimple = function(obj, evt, fn) {
    if (obj.addEventListener){ // W3C
        obj.addEventListener(evt, fn, false);
    }else if (obj.attachEvent) // Microsoft
        obj.attachEvent('on' + evt, fn);
}



var button = $('ppp'),
    content = $('content');


var init = function() {};

init.handleObj = button;
init.showbox = content;    
init.flag = false;
init.allowcls = false;    
init.open = function () {//open the content
        if (this.flag == false) {
            this.showbox.style.display = "block";
            this.flag = true;
            this.allowcls = true;
        }
    };
init.close =  function () { // close the content
        if (this.flag && this.allowcls) {
                this.showbox.style.display = "none";
                this.flag = false;
                this.allowcls = false;
        }
    };
init.load = function() { // button click
        //e = e ||window.event;
        //e.preventDefault();
        if (this.flag) {
            this.close();
        } else {
            this.open();
        }
    };
init.clickClose = function(e) { // document click

        if (!this.allowcls) {
            return;
        }
        e = e ||window.event;
        var target = e.target;
        if (target === this.showbox) { // button
            return;
        }       
         this.close();
};


addEventSimple(button, 'click', function (){
    init.load();//the code run here OK
})// error on here,but i don't know why?
addEventSimple(document, 'click', function (e){
    init.clickClose(e);
})

code in here :http://jsfiddle.net/DCty3/

  • 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-30T01:08:39+00:00Added an answer on May 30, 2026 at 1:08 am

    To toggle element display, you can use function like this:

    function toggleDisplay(id) {
        var elem = document.getElementById(id);
        elem.style.display = (elem.style.display != 'none' ? 'none' : '');
    }
    

    There is unlimited id toggle script too:

    function toggle() {
        for ( var i=0; i < arguments.length; i++ ) {
            var elem = document.getElementById(id);
            elem.style.display = (elem.style.display != 'none' ? 'none' : '' );
        }
    }
    

    With jQuery it will be just:

    function toggleDisplay(id) {
        $('#' + id).toggle();
    }
    

    I like getting fancy with objects, here is another multifunctional method:

    var display = {
        show : function() {
            for ( i=0; i < arguments.length; i++ ) {
                document.getElementById(arguments[i]).style.display = '';
            }
        },
        hide : function() {
            for ( i=0; i < arguments.length; i++ ) {
                document.getElementById(arguments[i]).style.display = 'none';
            }
        },
        toggle : function() {
            for ( i=0; i < arguments.length; i++ ) {
                var elem = document.getElementById(arguments[i]);
                elem.style.display = (elem.style.display != 'none' ? 'none' : '' );
            }
        }
    };
    

    To use it, do something like this:

    display.show('content','content2','content3');
    display.hide('content','content2','content3');
    display.toggle('content','content2','content3');
    

    For convenience, you can add another function to enable button:

    function toggleDisplayButton(bId, cId) {
        document.getElementById(bId).onclick = (function() {
            toggleDisplay(cId);
        });
    }
    window.onload = (function() {
        toggleDisplayButton('ppp','content');
    });
    

    To enable function, when user clicks “outside” the box, and it closes, there is used an easy trick, by creating another element in background, that covers all area around:

    <div id="wrapper">
        <button id="ppp">button</button>
        <div id="outerBox" style="display:none;"></div>
        <div id="content" style="display:none;">1</div>
    </div>
    

    CSS should make your container relative positioned and “trick box”, absolutely positioned, with maximum size of screen, and z-indexed under container like this:

    #content{width:100px;height:100px;background-color:#efefef;border:1px solid #555555;z-index:56;position:relative;}
    #outerBox{width:100%;height:100%;position:absolute;top:0;left:0;z-index:55;}
    

    And make it work like this:

    var display = {
        toggle : function() {
            for ( i=0; i < arguments.length; i++ ) {
                var elem = document.getElementById(arguments[i]);
                elem.style.display = (elem.style.display != 'none' ? 'none' : '' );
            }
        }
    };
    function toggleBox() {
        document.getElementById('ppp').onclick = (function() {
            display.toggle('content','outerBox');
        });
        document.getElementById('outerBox').onclick = (function() {
            display.toggle('content','outerBox');
        });
    }
    onload = function() {
        toggleBox();
    }
    

    The result looks like this.

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

Sidebar

Related Questions

I'm using this jQuery to hide a DIV: $(#slider).click(function() { $(.help).slideToggle(); $(#wrapper).animate({ opacity: 1.0
<div id=wrapper> <div id=header>...</div> <div id=main> <div id=content>...</div> <div id=sidebar>...</div> </div> </div> #wrapper {
In the following extJS page, when I click the button, the content of the
I have the following standard markup: <body> <header><div class=wrapper>Header</div></header> <div id=create>create something</div> <div class=wrapper>Content</div>
Here is my code: $('.button').click(function() { $(this).addClass('clicked'); $('.button').not(this).removeClass('clicked') .addClass('not_clicked'); $('#tool_window,#wrapper').not('#work_area').not('.button').addClass('outside'); }); $('.outside').click(function() { $('.clicked').removeClass('clicked');
I have a wrapper div with a height of 600px and a width of
Snippet of my CSS: #wrapper div.box { background: url('box-bg.png') left top repeat-y; } #wrapper
I have a main wrapper div that is set 100% width. Inside that i
I have the following DOM: <div class=qtip qtip-light qtip-active> <div class=qtip-wrapper> <div class=qtip-borderTop></div> <div
I've been struggling with this problem.. There is a wrapper div and it contains

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.