More on the Keyboard for a Web Page Calculator - The Fourth if (else if) block
(Page 5 of 5 )
The fourth if (else if) block is:
else if (keynum == "13") //for the equal sign
{
ID = "BEQ";
//call the showNum() function with the ID
operator(ID);
//give the button focus
document.getElementById(ID).focus();
}
The “if else” line checks to see if the Enter key was pressed. If it was, it assigns the ID (‘BEQ’) of the corresponding calculator button. The block then goes on to call the operator(ID) function using the ID. Remember that the operator(ID) function does the evaluation. It does it through this block.
We have pressed the Enter key on the keyboard. This has to be equivalent to clicking the corresponding button (=) on the calculator. So this corresponding button on the calculator has to receive focus. The last line of the block gives the button focus.
Now you may ask, “Is this not repetition? We handled the equal sign in the previous block. Why do it again?” Well, on the keyboard, the Equal Sign key does not have the same key code as the Enter key. The user may decide to press the Equal Sign key or the Enter key. So we have to handle both cases.
The next question you may ask is, why have I used (keynum == "13") and not (keychar == "Some Enter Character") in the last “if else” line? Systems may not retrieve the character.
We have come to the end of this section. We continue in the next part with scientific functions.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |