.onfocus = someFunction; problem...?

What is different about the following?

document.getElementById('myframeid').onfocus = someFunction;

document.getElementById('myframeid').onfocus = someFunction(100, 200);

I think this question might provide the answer I’m looking for.  However, if it does not I can provide more code as to where my specific problem is.  More or less all I need to point out at this point is that the first line of code works as it should.  That is, every time that ‘myframeid’ gets the focus, someFunction() runs properly.

However, in the 2nd line where I pass a parameter, every time ‘myframeid’ gets the focus the function does not run properly.  And yes, I am altering someFunction() to include the passed in parameter like someFunction(passedNumber).  My guess is that this is something to do with not being able to pass parameters in the .onfocus handler?

Andrew
LVL 1
rebiesAsked:
Who is Participating?
 
BatalfConnect With a Mentor Commented:
Try this instead:

document.getElementById('myframeid').onfocus = function() { someFunction(100, 200); }
0
 
BatalfCommented:
Try this:

document.getElementById('myframeid').onfocus = new Function ( someFunction(100, 200) );
0
 
rebiesAuthor Commented:
Nope, same result.

Andrew
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
absongConnect With a Mentor Commented:
1st line:
document.getElementById('myframeid').onfocus = someFunction;
you are assigning a FUNCTION to onfocus. So each time this element is focused, the function would be invoked.

2nd line:
document.getElementById('myframeid').onfocus = someFunction(100, 200);
you are assigning the RESULT of a function to onfocus. unfortunately onfucos does not take results.

I don't think there is a way to assign a function with parameters to onfocus. I think you can create a wrapper function, or do an evaluation thingy

wrapper:
function wrapper() {
   someFunciton(100, 200);
}
document.getElementById('myframeid').onfocus = wrapper;

evalution thingy (don't know exact syntax):
document.getElementById('myframeid').onfocus = 'function() someFunction(100, 200)';
0
 
rebiesAuthor Commented:
Sorry, checked the first one.  The second one does work as it should.

Can you do me one more favor and try to explain why?  To me the following line of code looks logical and I would think it works:

document.getElementById('myframeid').onfocus = someFunction(100, 200);

Andrew
0
 
rebiesAuthor Commented:
Cool, makes sense the way you described it absong!

Thanks both.  I'll split points, as Batalf gave me the code I need, and absong described the problem in English!

Andrew
0
All Courses

From novice to tech pro — start learning today.