1

Apologies if I appear a little "noobish" with events, but for whatever reason the following doesn't work for me:

var someDomRef = document.getElementByRef("refVal");
for(i=0;i<someDomRef.length;i++) { //or someDomRef.childNodes.length/someDomRef.TagRef.length
 someDomRef.onmouseup = function() {
  someDomRef.childNodes[i].onmouseover=function() {
   if(someRef.onmouseup) {
    //return false for the onmouseover handler of this(someDomRef.childNodes[i])
   }
  };
 };
}

Each time I release the mouse button after holding it upon someDomRef, I find a "onmouseover could not be assigned to undefined object" error in the JS console. Any help would be greatly appreciated for solving this problem (note: I know that I can assign another event handler outside of the onmouseover function to itself on the condition of someDomRef.onmouseup, but I'd like to know of a way to achieve this from within that onmouseover itself (I've also tried assigning var x = someDomRef.childNodes[i] and passing it through as an argument to the conditional clause for someRef.onmouseup, but this doesn't work either (albeit it doesn't return an error for this attempt)).

2
  • what is someValue where does it come from. Also i don't get your point what are you trying to do attach event handlers? Commented Jun 6, 2011 at 5:07
  • Apologies, check my edit for the amendment for what I was referring to with someVal. I was trying to change the handler for an object's event from within another handler for the same event of that object executed on the condition of an external event transpiring. Commented Jun 6, 2011 at 5:16

1 Answer 1

1

i is undefined when mouseup is called. You need to close over this with some closure functions:

var someDomRef = document.getElementByRef("refVal");
for(i=0;i<someval;i++) {
    (function(i) {
        someDomRef.onmouseup = function() {
            someDomRef.childNodes[i].onmouseover=function() {
                if(someRef.onmouseup) {
                    //return false for the onmouseover handler of this(someDomRef.childNodes[i])
                }
            };
        };
    })(i);
}

You may need another closure inside of the onmouseup function

Sign up to request clarification or add additional context in comments.

6 Comments

@James, note there's no need to discuss edits in the body of the post -- SO keeps an edit history for edited posts, so it's always easy to find what's original, what was added or removed, along with the checkin notes why. :)
Thanks. The syntax you're using for creating that function is unfamiliar to myself, and I'm somewhat absent of the necessities for searching of how and why pertaining to it (i.e. the (function(){})(); ) -- could you explain it a little? Thanks!
@user784446 it's called self invoking function. It is the definition of the function and it runs immediately after definition. So for each value in the SomeValue the function runs taking i as the paramerter. the rest is self explanatory i suppose
@user784446 it's used to create a "closure" that keeps the local variables (i in this case) around after they normally would have gone out of scope.
@sarnold - i actually added the body edit explanation to ensure my edit was long enough
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.