I understand that ch. 6,7, 8 are most important basics to understand. I'm getting confused as I read over them.
1. event listener and event handler are used form same thing? Please correct me if I am wrong. I get it that a function written to do something for an event is referred as either event listener or event handler.
2. Can I assign a function with argument for event handling? Like, I have to create a JavaScript game of 2 players. I want to disable/enable player's button to give each one a turn. I'm doubtful about the logic, but copying the code to show what I want to ask. How to handle such functions? or what am I missing?
function dice() {
'use strict';
var diceno = Math.floor(Math.random()*6 + 1);
document.getElementById("dice").innerHTML = diceno;
}
function toggleButton(bt) {
'use strict';
dice();
b.disabled = true;
if(b.value == 'b2') { //here b.value does not give the name or id of button, as button is a input, and it's value property isundefined.
console.log(b.value);
document.dcForm1.b1.disabled = false;
} else {
console.log("b1");
document.dcForm1.b2.disabled = false;
}
}
window.onload = function() {
'use strict';
document.getElementById('b1').onclick = toggleButton; //This thing does not work, as it requires a parameter.
document.getElementById('b2').onclick = toggleButton;
}
Event Handling And Functions
Started by
sonal
, Mar 27 2012 2:12 PM
3 replies to this topic
#1
Posted 27 March 2012 - 2:12 PM
#2
Posted 28 March 2012 - 7:21 AM
1) Yes, an event listener and an event handler are essentially the same thing.
2) Yes, you can send arguments to functions by using anonymous functions. For example:
That answer your questions?
2) Yes, you can send arguments to functions by using anonymous functions. For example:
elem.onmouseover = function () {
someFunction(arg);
};
That answer your questions?
#3
Posted 28 March 2012 - 2:18 PM
2) Yes, you can send arguments to functions by using anonymous functions. For example:
elem.onmouseover = function () { someFunction(arg); };
Ok, got it. Create an another (anonymous) function assigning an event which can take a function with argument.
Thanks
#4
Posted 28 March 2012 - 5:39 PM
Exactly. Keep in mind that using an anonymous function will not preserve the context of "this" in the function being called though, so you have to explicitly pass "this" as an argument, if you want to use it in the function being called.











