Help on binding mouseover with jquery.

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
I thought that was too obvious to require explicit statement as there were only three js files on my page and two of them were the jquery and its ui repectively, and no one here expect that I wrote both of them too, right?
It's best not to make assumptions about what's obvious. It wasn't a problem here, but too often have I seen someone asking for help make an assumption about a key point. Break that habit while you can. Always be explicit about where an error occurs, and what the error is, even if they seem obvious. If nothing else, this shows you're actively working on the problem and not simply trying to pass it on to someone else.

Not that every bit of information about the problem needs to be included. Extraneous information should be filtered out. The filtering impulse is a good one, it's just a matter of tuning your filters.

I'm not being rude here, I know that I have no right to be-maybe I'm just a lazy little boy,bad lazy-in your words. Anyway, guys like you here help me a lot, and 'm thankful to that; so I'll follow your advice and make the info as explicit as possible in future.
I didn't read it as rude, even though I was myself certainly being gruff. The link to the "Laziness, Impatience and Hubris" page was intended to cut my gruffness somewhat.

So then in order to avoid the global, if I replace the call to mbind by:
Code:
$("#"+tp).bind('mouseleave', slidein(this));
It still produces the same result. Can you explain what is the correct method and what I'm doing wrong? I know adding a class as you said is better, but I wanna know why my code fails.
If that line is run from within slider.build, then this should refer to a slider. If it isn't working as you'd expect, I'd look to the implementation of the function returned by slidein. Nothing from the prototype implementation of slidein jumps out at me as being incorrect. Try a JS debugger, such as Firebug or that built in to Safari or Chrome (the Chrome debugger offers a variety of breakpoint types, including breaking on DOM events).

Note that the line wouldn't work in mbind (even if you rename variable tp to sId) as mbind is invoked as a free function (i.e. not a method), so this in mbind refers to the global object (which is window in browsers; this is initially undefined when the function is called, so it's assigned the global object as per ECMAScript-262 10.4.3).

Pop quiz: what's this in an event handler?

Is there any disadvantage with that?
I LOVE numbers, whats wrong with them?
Magic incantations are notoriously dangerous to alter. Forget to change something or change the wrong thing and you'll find yourself transported far to the North to the Land of Cutz, where customs are strange, misunderstandings are many and inconsistencies abound. See "magic number", sense 1. What happens if you discover a bug and need to change the states or state transition function? (On the subject of transition functions, unifying state transitions into a single function or an array of functions (indexed by the current state) is much easier to manage.) See also "Unnamed numerical constants" on Wikipedia and Magic Number in Portland Pattern Repository's wiki (for some cruel humor, read "Replace Magic Number With Symbolic Constant", also in the latter).

You're some sort of a perfectionist ain't you? Well, personally I like being one at times, but you are way ahead.:)
When it comes to computers, you have to be. They aren't capable of forgiving errors. This is codified in half of Postel's Law: "Be conservative in what you send; be liberal in what you accept."
 

Teensweb

New Member
Messages
352
Reaction score
1
Points
0
I guess I should be giving up on the stupid proto; I am not getting much ahead even with firebug and my free server at firebolt.co.cc is down.
BTW, one question: Is binding an event to all items of a class any beneficial over looping through each element manually to do the same?
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
BTW, one question: Is binding an event to all items of a class any beneficial over looping through each element manually to do the same?

It's cleaner and simpler and will take less time to implement.
And shortened development time is nothing to sneeze at. For professional developers, it's about as important as execution time (not that this means one is picked over the other when there's a conflict, just that a significant amount of attention is paid to shortening development time).

Using a class is conceptually cleaner. All the elements the listener is being bound to conceptually belong to a class (hoverable menus), so make this explicit.

Subscribing each listener separately is not to make use of jQuery's power (in this case, jQuery's selectors), at which point you have to ask why use something so powerful? Why not a simpler JS library that merely presents a unified, cross browser API?

Finally, subscribing each element individually has no benefits over subscribing all at once.
 
Last edited:
Top