L
lawrence
I'm trying to gain a better understanding of javascript by studying
examples. I noticed this in an online tutorial. I don't get the use of
"this".
You might want to add some event handler for an event to an element to
which there is already an event handler attached. That is possible by
storing the old event handler somewhere and then writing a new
function
which calls the old event handler.
For example suppose you have a document with buttons with onclick
handlers
<INPUT TYPE="button" NAME="aButton" VALUE="praise God"
ONCLICK="alert('Hail the Kibo');"and you want add script that disables the event handler temporarily
then
you store the old event handler
var button = document.formName.aButton;
button.oldOnClick = button.onclick;
and write your new click handler which calls the oldOnClick when
necessary
function disabledButton (evt) {
if (this.disabled)
return false;
else
if (this.oldOnClick)
this.oldOnClick(evt);
}
button.onclick = disabledButton;
Now you can set
button.disabled = false;
to disable the button and
button.disabled = true;
to enable it. Complete example:
In the these two lines:
function disabledButton (evt) {
if (this.disabled)
Where did the "this" come from and what does it refer to? Also, where
did the property "disabled" come from???
examples. I noticed this in an online tutorial. I don't get the use of
"this".
You might want to add some event handler for an event to an element to
which there is already an event handler attached. That is possible by
storing the old event handler somewhere and then writing a new
function
which calls the old event handler.
For example suppose you have a document with buttons with onclick
handlers
<INPUT TYPE="button" NAME="aButton" VALUE="praise God"
ONCLICK="alert('Hail the Kibo');"and you want add script that disables the event handler temporarily
then
you store the old event handler
var button = document.formName.aButton;
button.oldOnClick = button.onclick;
and write your new click handler which calls the oldOnClick when
necessary
function disabledButton (evt) {
if (this.disabled)
return false;
else
if (this.oldOnClick)
this.oldOnClick(evt);
}
button.onclick = disabledButton;
Now you can set
button.disabled = false;
to disable the button and
button.disabled = true;
to enable it. Complete example:
In the these two lines:
function disabledButton (evt) {
if (this.disabled)
Where did the "this" come from and what does it refer to? Also, where
did the property "disabled" come from???