www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 05-13-2007, 04:42 PM
    dmeglio dmeglio is offline
    Registered User
     
    Join Date: May 2007
    Posts: 2
    Closing a floating div layer

    I'm trying to make a floating div. Actually, something very similar to the "Search" floating div on this forum. Anyway, I want to make it so when the user clicks somewhere outside of the search div, it closes itself (again, like the search box on this forum does). Does anyone know how I can detect a click that occurs anywhere outside of the search box?
    Reply With Quote
      #2  
    Old 05-14-2007, 07:07 AM
    gil davis's Avatar
    gil davis gil davis is offline
    Nobody important
     
    Join Date: Nov 2002
    Posts: 4,456
    You might have success using onblur().
    Reply With Quote
      #3  
    Old 05-14-2007, 08:52 AM
    dmeglio dmeglio is offline
    Registered User
     
    Join Date: May 2007
    Posts: 2
    onblur doesn't trigger for div's. According to the HTML 4 spec, it only triggers for, A, AREA, LABEL, INPUT, SELECT, TEXTAREA, and BUTTON. IE triggers it for a DIV, but not Firefox.
    Reply With Quote
      #4  
    Old 05-14-2007, 09:00 AM
    Orc Scorcher Orc Scorcher is offline
    S.P.Q.R.
     
    Join Date: Mar 2005
    Posts: 767
    Register a click event listener for the document that checks whether the click occured in your div. Quick & dirty example
    Code:
    <div id="box"
     style="height: 3em; position:absolute; top: 20%; left: 15%; border: 3px double">
    	<p>Click anywhere outside this box to close it.
    </div>
    <script>
    
    document.onclick = function (e) {
    	e = e || event
    	var target = e.target || e.srcElement
    	var box = document.getElementById("box")
    	do {
    		if (box == target) {
    			// Click occured inside the box, do nothing.
    			return
    		}
    		target = target.parentNode
    	} while (target)
    	// Click was outside the box, hide it.
    	box.style.display = "none"
    }
    </script>
    __________________
    Stop thinking, start drinking.
    Reply With Quote
      #5  
    Old 02-29-2008, 11:03 PM
    uberzev uberzev is offline
    Registered User
     
    Join Date: Feb 2008
    Posts: 1
    Quote:
    Originally Posted by Orc Scorcher
    Register a click event listener for the document that checks whether the click occured in your div. Quick & dirty example
    Code:
    <div id="box"
     style="height: 3em; position:absolute; top: 20%; left: 15%; border: 3px double">
    	<p>Click anywhere outside this box to close it.
    </div>
    <script>
    
    document.onclick = function (e) {
    	e = e || event
    	var target = e.target || e.srcElement
    	var box = document.getElementById("box")
    	do {
    		if (box == target) {
    			// Click occured inside the box, do nothing.
    			return
    		}
    		target = target.parentNode
    	} while (target)
    	// Click was outside the box, hide it.
    	box.style.display = "none"
    }
    </script>
    Great script Orc, any chance of getting a version that has a link to re-open the box?
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 04:08 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.