Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-05-2011, 12:59 AM   PM User | #1
maxhud
Regular Coder

 
Join Date: Mar 2011
Posts: 147
Thanks: 35
Thanked 0 Times in 0 Posts
maxhud has a little shameless behaviour in the past
On click with div

How can I use onclick with the <div> tag?
maxhud is offline   Reply With Quote
Old 04-05-2011, 01:03 AM   PM User | #2
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Code:
<div onclick="alert('div was clicked');">Click me</div>
bullant is offline   Reply With Quote
Users who have thanked bullant for this post:
maxhud (04-06-2011)
Old 04-05-2011, 05:59 AM   PM User | #3
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 710
Thanks: 31
Thanked 128 Times in 119 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
If you use onclick with a div, be aware that clicks bubble- you will be handling clicks from any child nodes in the div as well as from the div.
mrhoo is offline   Reply With Quote
Users who have thanked mrhoo for this post:
maxhud (04-06-2011)
Old 04-05-2011, 07:59 AM   PM User | #4
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Quote:
If you use onclick with a div, be aware that clicks bubble- you will be handling clicks from any child nodes in the div as well as from the div.
You can stop the bubbling though so that the onclick fires only on the parent div and not the child elements as well.
bullant is offline   Reply With Quote
Users who have thanked bullant for this post:
maxhud (04-06-2011)
Old 04-05-2011, 09:22 AM   PM User | #5
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Quote:
Originally Posted by mrhoo View Post
If you use onclick with a div, be aware that clicks bubble- you will be handling clicks from any child nodes in the div as well as from the div.
That's for IE. In old Mozilla model events are capturing (from parent to child) not bubbling (from child to parent). W3C standard model says now that there are 2 phases: first the capturing, and when the event reaches the "bottom", the bubbling phase is starting backwards
So that, yes, events can be stopped, but in the bubbling phase:
Code:
function doSmth(e){
var e = e||window.event;
e.cancelBubble = true;//IE model
if (e.stopPropagation) e.stopPropagation();//W3C model
}
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Users who have thanked Kor for this post:
maxhud (04-06-2011)
Old 04-06-2011, 12:01 AM   PM User | #6
maxhud
Regular Coder

 
Join Date: Mar 2011
Posts: 147
Thanks: 35
Thanked 0 Times in 0 Posts
maxhud has a little shameless behaviour in the past
bubbling shouldn't be an issue.

thanks for the help guys!
maxhud is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:06 PM.


Advertisement
Log in to turn off these ads.