Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onclick exluding elements 2

Status
Not open for further replies.

Graeme06

Technical User
Jun 6, 2006
60
Hi, I have an AJAX application with a div called "floating" that contains a menu and the div is only visible at certain times.

Here is my problem: I want it to disappear whenever someone clicks on the background, any form elements, or basically anywhere EXCEPT that menu box and one specific text box. Is there any easy way to accomplish this?

Thanks.
 
Graeme, I suppose you could check to see what element is being clicked and if it's not your div then close it:

Code:
function checkwhich(ev) {
obj=(window.external) ? event.srcElement : ev.target;
alert(obj.id)
//alert(obj.nodeName) -- to check which element
}
document.onclick=checkwhich;

I've a funny feeling this will only work in IE though. Sorry can't check FF as at work.

The part I've commented out will show you what element was clicked i.e. <body>, <div>, <a> etc...

Hope this helps

Nick
 
Thanks, but I'm having a bit of trouble understanding this code (Javascript is not my best language).

The lines:
alert (obj.id)
alert(obj.nodeName)

What are they doing?
 
Those two lines will create a JavaScript popup window telling the (obj.id) id attribute value of the object and (obj.nodeName) the HTML tag name.

 
Just got around to testing that, and it works perfectly (and in a Mozilla based browser - haven't been able to test IE yet). I just made a variable storing obj.id and did a conditional on the ones I wanted to exclude. Thanks both of you for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top