Topic: Need help - Javscript onclick event issue Pages that link to <a href="https://ozoneasylum.com/backlink?for=30173" title="Pages that link to Topic: Need help - Javscript onclick event issue" rel="nofollow" >Topic: Need help - Javscript onclick event issue\

 
Author Thread
paritycheck
Bipolar (III) Inmate

From: you tell me
Insane since: Mar 2004

IP logged posted posted 04-05-2008 15:08 Edit Quote

Hi guys need some help here pronto. I have a div whcih nests some other divs like this

code:
<div id="container" onclick="dosomething()">
  <div id="column01">....</div>
  <div id="column02">....</div>
  <div id="column03">....</div>
</div>



The thing is that when I click on the container div - I've put in the onclick event of that div a function call to be executed. Now the thing is that lets say even if I click on any of the divs within the container i.e. column01, column02, column03 - it still fires the onclick event on the container div. I would like that if someone clicks on the divs column01 and divs column02 that the dosomething() function as described in the onclick event of the container div should NOT be executed... how do I do this?

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

IP logged posted posted 04-05-2008 17:13 Edit Quote

You have to check that the element that fired the event ( event.target in the DOM2 standard, event.srcElement in non standards compliant browsers ) is element to which the event handler is attached ( event.currentTarget ).

code:
onload = function()
{
	document.getElementById('container').onclick = function( event )
	{
		var	event	= event||window.event,
			src	= event.target||event.srcElement;
		if( src==event.currentTarget )
		{
			//	hooray!
		}
	}
}

And forget about putting event listeneres inside the markup.

HTH



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu