1

First of all, I shall say that I am using an old version of the jQuery library (namely 1.4.2). Updating to jQuery 1.7 will take some time (especially to hunt the bugs), so I am looking for a solution which works in 1.4.2.

I want to write a function :

function _setupGui($domNode, selector){
  $domNode
    .delegate(selector, 'mouseenter', function(){ /* do smth awesome */ }
    .delegate(selector, 'mouseleave', function(){ /* do smth awesome */ }
    .delegate(selector, 'click', function(){ /* do smth awesome */ }
}

and be able to give a selector which allows to select "self".

Is there a selector, which you can pass to delegate, which allows to select the node holding the delegation ?

In jQuery 1.7, $domNode.on('click', null, function(){/*handler*/}) does just that, and is the actual implementation of $domNode.delegate(null, 'click', function(){/*handler*/}). However, in 1.4.2, the latter does nothing.

Is this a change in semantics with previous versions ?

2
  • How is that different than just adding a handler to the element instead of delegating? Maybe I'm misunderstanding. Commented Dec 15, 2011 at 18:02
  • @James: you're not, it's just about having a single entry point to bind events
    – LeGEC
    Commented Dec 15, 2011 at 18:40

1 Answer 1

1

What I think you really want is to attach the handlers directly the the element and not use delegate at all. You could just handle this yourself. Something like this perhaps, where passing "self" means just attach the events:

function _setupGui($domNode, selector){
    if(selector == "self"){
        $domNode
          .mouseenter(function(){ /* do smth awesome */ })
          .mouseleave(function(){ /* do smth awesome */ })
          .click(function(){ /* do smth awesome */ })
    }else{
        $domNode
          .delegate(selector, 'mouseenter', function(){ /* do smth awesome */ })
          .delegate(selector, 'mouseleave', function(){ /* do smth awesome */ })
          .delegate(selector, 'click', function(){ /* do smth awesome */ })
    }
}
1
  • What I want to do is attach the handlers directly to the element, but using a consistent means that doesn't get stuck in the bind() vs. live() vs. on() vs.... debate. I think on() was an attempt to do this, but IMHO it amounts to sugar-coating, and you know bugs love sugar. I have settled on delegate() because it is the underdog in the fight and everyone leaves it alone. I let everything bubble up to the body or document, because the performance hit is negligible when you consider it mostly happens when a human does something, which isn't that often in the computers time scale.
    – DaveWalley
    Commented Aug 22, 2014 at 21:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.