S
shuchow
Hi, sorry for the basic question, but can someone explain to me some
basic event attachment process. Say I have a function,
dynamicallyCreateElement(), that creates an element. I want that new
element to have an onclick that calls another function. So it looks
like this:
function dynamicallyCreateElement() {
....
element.onclick = myFunction;
....
}
function myFunction() {
...
};
The problem is that myFunction will be called during the execution of
dynamicallyCreateElement(), and not just the onclick for the element.
Instead, I have tod o this:
function dynamicallyCreateElement() {
....
element.onclick = function() { myFunctions())_;;
....
}
Why does the first one automatically execute myFunction, and what
exactly does
element.onclick = function() do?
basic event attachment process. Say I have a function,
dynamicallyCreateElement(), that creates an element. I want that new
element to have an onclick that calls another function. So it looks
like this:
function dynamicallyCreateElement() {
....
element.onclick = myFunction;
....
}
function myFunction() {
...
};
The problem is that myFunction will be called during the execution of
dynamicallyCreateElement(), and not just the onclick for the element.
Instead, I have tod o this:
function dynamicallyCreateElement() {
....
element.onclick = function() { myFunctions())_;;
....
}
Why does the first one automatically execute myFunction, and what
exactly does
element.onclick = function() do?