.delegate()

.delegate() event

The .delegate() method binds a handler independent of whether the DOM element exists.


<html> 
    <head> 
        <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script> 
        <script> 
            $(function(){ <!--from   w  w w  .ja  va2  s . c om-->
                $("body").delegate("p","click",function(){ 
                    document.writeln('ouch'); 
                }).css("color", "green"); 
            }); 
        </script> 
    </head> 
    <body> 
        <p>Hit Me! ja va2s.com</p> 
    </body> 
</html>

Click to view the demo

The code above generates the following result.

.delegate()
Home »
  Javascript Tutorial »
    jQuery Reference »
      Event
Javascript Tutorial Event
Event
Event attributes
event.keycode
event.clientX/clientY
pageX/pageY
Cancel event
Event target
bind
.blur()
.change()
.click()
.error()
.dblclick()
.delegate()
.die()
.focus()
.hover()
.keydown()
.keypress()
.keyup()
.live()
.load()
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseover()
.mouseout()
.mouseup()
.off()
.on()
.one()
.ready()
.resize()
.scroll()
.select()
.submit()
.toggle()
.trigger()
.triggerHandler()
.unbind()
.undelegate()
.unload()