![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
When i'm using the following, everything works well. butt1.on ('click', function () {sameStuff ('1')}); butt2.on ('click', function () {sameStuff ('2')}); Now, i wish to compact it into one call, instead of two anonymous functions. The problem is that i'm required to send in a function as the second parameter. If i use the following i get only a return back, not typed as function. butt1.on ('click', function ('1')); How can i enter as the second parameter BUT with a specified parameter in it? Is it doable at all? |
#3
| |||
| |||
|
|
When i'm using the following, everything works well. butt1.on ('click', function () {sameStuff ('1')}); butt2.on ('click', function () {sameStuff ('2')}); Now, i wish to compact it into one call, instead of two anonymous functions. The problem is that i'm required to send in a function as the second parameter. If i use the following i get only a return back, not typed as function. butt1.on ('click', function ('1')); How can i enter as the second parameter BUT with a specified parameter in it? Is it doable at all? |
#4
| |||
| |||
|
|
When i'm using the following, everything works well. butt1.on ('click', function () {sameStuff ('1')}); butt2.on ('click', function () {sameStuff ('2')}); Now, i wish to compact it into one call, instead of two anonymous functions. The problem is that i'm required to send in a function as the second parameter. If i use the following i get only a return back, not typed as function. butt1.on ('click', function ('1')); How can i enter as the second parameter BUT with a specified parameter in it? Is it doable at all? According to Core JavaScript 1.5 Reference, you cannot: To pass parameters to an event handler, the handler must be wrapped into another function" document.form1.button1.onclick = function() { setBGColor('some value'); }; http://developer.mozilla.org/en/docs...event_handlers |
#5
| |||
| |||
|
|
When i'm using the following, everything works well. butt1.on ('click', function () {sameStuff ('1')}); butt2.on ('click', function () {sameStuff ('2')}); Now, i wish to compact it into one call, instead of two anonymous functions. The problem is that i'm required to send in a function as the second parameter. If i use the following i get only a return back, not typed as function. butt1.on ('click', function ('1')); How can i enter as the second parameter BUT with a specified parameter in it? Is it doable at all? I'm having trouble understanding what you're trying to do. Are you perhaps trying to define a single function that can act as the handler for both button clicks, and do something useful? If that's the case, just define the function before the two calls to "on", assigning it to a variable, and reference that variable in the two "on" calls. In the common event handler, you'll probably have to know which button was clicked, so you'll have to get the event object (either passed in as the single parameter, or the global event object) and get the target property. Does that help? |

#6
| |||
| |||
|
|
When i'm using the following, everything works well. butt1.on ('click', function () {sameStuff ('1')}); butt2.on ('click', function () {sameStuff ('2')}); Now, i wish to compact it into one call, instead of two anonymous functions. The problem is that i'm required to send in a function as the second parameter. If i use the following i get only a return back, not typed as function. butt1.on ('click', function ('1')); How can i enter as the second parameter BUT with a specified parameter in it? Is it doable at all? |
#7
| |||
| |||
|
#8
| |||
| |||
|
|
K Viltersten escribió: When i'm using the following, everything works well. butt1.on ('click', function () {sameStuff ('1')}); butt2.on ('click', function () {sameStuff ('2')}); Now, i wish to compact it into one call, instead of two anonymous functions. The problem is that i'm required to send in a function as the second parameter. If i use the following i get only a return back, not typed as function. butt1.on ('click', function ('1')); How can i enter as the second parameter BUT with a specified parameter in it? Is it doable at all? According to Core JavaScript 1.5 Reference, you cannot: |
|
To pass parameters to an event handler, the handler must be wrapped into another function" document.form1.button1.onclick = function() { setBGColor('some value'); }; http://developer.mozilla.org/en/docs...event_handlers |
#9
| |||
| |||
|
|
You can use something like this: function makeSameStuff(n) { return function() { sameStuff(n); }; } butt1.on("click", makeSameStuff("1")); butt2.on("click", makeSameStuff("2")); |
#10
| |||
| |||
|
|
Do you mean that switching between quotation marks and apostrophes makes that difference? No, it does not matter whether you use single or double quotes. The |
![]() |
| Thread Tools | |
| Display Modes | |
| |