views:

27

answers:

3

I've got a delegate statement that works like so:

$("body").delegate("tr[type='option']",'mouseenter',function(){

The problem is that it's grabbing elements from tables I don't want. So I tried:

$("body").delegate("table[class='ms-MenuUI'] > tr[type='option']",'mouseenter',function(){

Which isn't working at all (though I'm not getting any console errors). Just wondering how I can tighten this up so it's only grabbing table rows from the specific table I want.

NOTE: the table does not exist in the DOM on page load, and is dynamically created/destroyed after the doc is ready, thus the need for delegate to begin with.

EDIT: As per my comment below, I'm using [] because the attribute of the parent is variable, and it's my understanding that they should work interchangeably with the attribute short-hand (i.e. '.'). A sample of the dynamic code would be:

$('body').delegate('table[' + parentAttribType + "='" + parentAttribValue + "'] > tr[" + rowAttrbType + "='" + rowAttribValue + "']"), 'mouseenter', function(){

Thanks!

+1  A: 

You should use the class selector . for classes, not the attribute selector []:

$("body").delegate("table.ms-MenuUI > tr[type='option']",'mouseenter',function(){

Also, your browser may be introducing a tbody element. If you can, remove the >.

lonesomeday
Is that mandatory? My sample is hard-coded, but the attribute being selected is dynamic and won't always be class. Using [] allows me to pass the attribute type as a variable easily in the selector, and afaik the functionality should be the same. I'll update with another example.
patrickgamer
Yes, it should work, it's just ugly syntax. I fancy the `tbody` issue may be the cause of your issue.
lonesomeday
A: 

It should work:

$("table[class=ms-MenuUI] tr td").live('mouseenter',function(){
  /* yout code*/
});

The live is like bind but works on dhtml.

madeinstefano
+1  A: 

Use this, the event handler gets bound to the table using 'tr td' as its event target.

$('table[class=ms-MenuUI]').delegate('tr td', 'mouseenter', function() {

});

FYI -

If you are going to use $('body').delegate('', ) then its probably better to use .live() which binds event handlers to the document level anyways.

EDIT: Sorry didn't read your entire post, delegate the event to an element that either contains the table, or use .live().

John Strickler
@John It's never better to use `live`. (1) It has horrid syntax, (2) it requires an extra selector when doing the delegation.
lonesomeday
this looks like it's the right solution. I disagree that delegate is *always* better than live - it's a matter of design constraints and solution needs imo...
patrickgamer
Thanks John, I'm going to move forward with live()
patrickgamer
@John How? They are functionally equivalent, except for live's odd syntax and a slight performance increase for `$(document)` over `$('tr td')`.
lonesomeday
If you decide to bind a handler to the document level what would you prefer? $(document).delegate('tr td', 'click', fn); or $('tr td').live('click', fn); You are right they same thing. For simplicity, live may be the choice for certain people. Delegate() was created after live() purely to fix the ugly syntax of using $('tr td', $('table')[0]).live('click', fn). I suggested .live() in the above answer because he was binding a handler to the <body> when he could just use .live() if he was going to be doing that. Questions? Concerns?
John Strickler
I chose John's answer because of the comment he just made. I don't want to bind to the document and I tend to stand apart from the general census on syntax 'prettiness' of live() - I like it just find. I won't start a rant about syntax here; bottom line is that this answer is what got me to my solution the fastest. Thanks
patrickgamer