javascript - How to hook one event for block with many elements -
i have markup:
<fieldset class="checkbxr full small" onclick="alert('test')"> <label for="" class="fat_label">category</label> <input type="checkbox" name="true_checkb" id=""> <span class="dmy_checkb"></span> </fieldset>
this dummy checkbox solution. ok, need hook 1 onclick
event whole fieldset
. if below have 3 alerts, of elements inisde, event fires 3 times. how set fieldset
, make 1 event call? thx help.
this fiddle js , css: http://jsfiddle.net/vet6y/
you need cancel event bubbling depending on clicked.... here script (in pure javascript - easier in jquery - can check if there class on event source example , cancel then). script checks if tag clicked fieldset, if not cancels bubble, if pops alert.
fiddle http://jsfiddle.net/yk3tu/2/
code:
html:
<fieldset class="checkbxr full small" onclick="javascript:dosomething()"> <label for="" class="fat_label">category</label> <input type="checkbox" name="true_checkb" id=""> <span class="dmy_checkb"></span> </fieldset>
javascript:
function dosomething(e) { var targ; if (!e) var e = window.event; if (e.target) targ = e.target; else if (e.srcelement) targ = e.srcelement; if (targ.nodetype == 3) // defeat safari bug targ = targ.parentnode; console.log(targ) if (targ.tagname!="fieldset") { cancelbubble(e) } else { domything() } } function cancelbubble(e) { var evt = e ? e:window.event; if (evt.stoppropagation) evt.stoppropagation(); if (evt.cancelbubble!=null) evt.cancelbubble = true; } function domything(){ alert("this test") }
Comments
Post a Comment