/    Sign up×
Community /Pin to ProfileBookmark

Differentiating between divs and document

Hi,
I have two divs with an Onclick method each, which the only thing that I do is get the name of the clicked div and put the name in a textbox.

The only thing I´d like to do is be able to identify when a click has occured outside of the divs, and on the body, so in the textbox i´d write (“No selection made”).

[CODE]
<div class=”div1″ id=”div1″ onclick=”ObjectClick(this)”>
</div>

<div class=”div2″ id=”div2″ onclick=”ObjectClick(this)”>
</div>

function ObjectClick(object)
{
if(object)
{
object.onmousedown = ObjectClicked(object);
};
}

function ObjectClicked(object)
{
if(object)
{
draggableObject = object;
var selected = document.getElementById(“txtSelectedObject”);
selected.value = “Hi you clicked” + ” ” + object.id;
}
}

[/CODE]

I tried adding the onclick event to the body, but this didn´t work,..

Any suggestions for this javascript beginner?
Cheers

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorJan 29.2008 — What is, in fact, your final aim? validating for select elements?
Copy linkTweet thisAlerts:
@RaulandauthorJan 29.2008 — Hi, the aim is to identify when a click has ocurred outside of any of the two divs.

With only purpose to do a few tests and learn,..?
Copy linkTweet thisAlerts:
@mrhooJan 29.2008 — You can handle all the clicks on the page by letting them all bubble up to the body, and examining the target or srcElement of the event.

I'm using double click for the event, but suit yourself:

[CODE]onload= function(){
document.body.ondblclick= function(e){
e= window.event || e;
e= e.target || e.srcElement;
if(e.id)alert(e.id);
else alert('You clicked on the '+e.nodeName+ ' element');
}
}[/CODE]
Copy linkTweet thisAlerts:
@RaulandauthorJan 30.2008 — Thanks
×

Success!

Help @Rauland spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...