Changing event handlers from JavaScript. Calculator algorithm.

Changing event handlers from JavaScript.

Assume we have an element (for example, button) on our page and we assigned an event handler to the element (for example, onClick), but this button should not always work or should do different things. What things depends on some other events generated by user. Of course, we can create a very sophisticated event handler that can take care of all possible combinations, but some time this is not the best way. In this section we will discuss how we can change an event handler from JavaScript. Thus, our problem is depending to other events occurrence change the event handler for our element. Let's talk about event handler onClick for an element button with ID btn. We already know how to access this element:
document.all['btn']
We know learn that when we assign a handler onClick in the HTML tab button the web browser creates property onclick of this element.
Thus, all we need to do is just assign a new value to this property. For example:
document.all['btn'].onclick = null;
In this example we assigned to the event handler a special keyword null, which tells the browser that currently this element does not have any handler for this event. This following link cancels event handler of the previous button. Try to click on the upper button after you clicked on the link and make sure it's no longer working.
Unlike onClick inside HTML tags (which can be written with mixed lower/upper case characters), onclick in JavaScript should be written in lower case. Otherwise JavaScript will create a new property with name, let's say ONClick, but the old onclick will stay and work.

Now when we know how to destroy an event handler let's talk about how to change it. To change an event handler all we need to do is to assign it a new function. How to do this? First of all we need to mention that Function is one more JavaScript object. Every time we create a function JavaScript creates an new instance of this object. How can we create a new function? One of the methods we saw before we just didn't talk about it as of creation of anew object. We can create a new function lik ewe always did:

function new_fun()
{
  alert("Now I'm printing this message");
}
If we already have a function created this way we can assign property onclick the name of the function:
document.all['btn'].onclick = new_fun;
Try click on this link and see that the button above works completely different now.

Now we can do two different things:

  • completely remove an event handler
  • or assign a new one using already written function.

    It turns out that there is one more method to do that. To use this method we need to learn another way of creation a Function object. As usual object Function can be created with operator new. The general syntax is:

    var fun_name = new Function( "argname1", "argname2", ..., "last argname", "statements" );
    For example:
    var another_fun = new Function( "msg", "alert(msg);" ); 
    
    Created by like this function can be assigned by its name (another_fun in our case) or directly without creating an object with this name:
    document.all['btn'].onclick = new Function( "alert('Now I will print another message');" );
    
    Click on this link to see if it works.

    The following example contains the complete code that shows all these method applied to a set of functions. Please see how it's written and what it does. Besides changing event handlers pay attention how we can change the look of the buttons and how it is done.

    Notes about Calculator project

    In this section we need to discuss several issues about the Calculator project. First of all, we would like to have 0 our default value of the display input string. That is, when you load your calculator page you should see zero on the display. When user starts clicking digit buttons zero should disappear and the digits should be displayed. For example, if we opened the calculator page and clicked "2" and "7" we should see "27" on the display, not "027". That is, we don't have to always add characters to the display. Sometimes we have to replace them. To implement this it's convenient to have a boolean variable, which indicates what to do with the input add or replace the currently displayed string.

    More difficult detail is when to perform the calculation. Let's look how usual calculator works (at this [point you are expected to open standard Windows or Linux calculator) and try to calculate 22+44 Please notice the following:

    1. when you press the first 2 initial zero disappears
    2. when you press + it doesn't appear on the display
    3. when you press the first 4 22 disappears
    4. when you press the second 4 nothing happens you have to click either = sign or press another operation button
    We have discussed how to implement the first issue. To deal with the second issue we don't have to do much actually. Just memorize the operation pressed by the user and reset the boolean variable to the value indicating that next time we need to replace not add. By doing this by the way we'll take care about the third issue as well. To take care about the last mentioned issue and perform the arithmetic we need to wait until user presses the equal sign or another operation and then perform the stored operation. When the operation performed we need to display the result and again set the boolean variable to its replace value. We, of course, must remember the second operation user pressed to perform the first one.

    Another difficult point in this project may be the notation part. We already discussed the issue about conversion from string to a number and the other way around, but we should remember that the notation refers only to the currently displayed number and to the result of the operation not to the stored number. Because the user can switch to a different notation while entering the second number. Taking all of these into consideration it would be much more convenient to store numbers as numbers not as strings. Thus, when user presses an operation button we need to convert the display string into number using the right base. It's also important to remember about base when we are about to display a results of operation. We first need to convert it from number into a string using the right base and then display it to the user.

    How to deal with log text area is left to the reader.

    The following example illustrates the ideas discussed above including redefining event handlers.