To achieve the first goal all we need to do is to create a corresponding style and apply it to the input element. This style can look like this:
.big_string {
color: darkblue;
font-size: 200%;
}
and we can apply this style using tag input with the correct value of the
parameter class:
<input type=text class=big_string>
For defining new behavior of the buttons we need to define a handler of the event Click which occurs when user clicks on a button. For the first three buttons this event handler should do exactly the same thing add a particular character to the input string. Thus, we can create a new function named add_character(), which takes the character to add as an argument and adds it to the string. The defining the event handler and making it use this function can be done the following way:
<input type=button value='A' onClick="add_character('A');">
<input type=button value='B' onClick="add_character('B');">
...
Now we need to declare the function add_character() and make it work exactly the way we want it.
To do that we first need to get an access to the input string element. As we know from a
previous lecture we can access any element of a form by its name and we can access
any form of our document by also by name. Thus, we need to name the form and the input element:
| Code | Result |
|---|---|
<form name=calc_form> <input type=text class=big_string name=display_string value="0"> </form> |
document.forms["calc_form"].elements["display_string"].valueor shorter:
document.calc_form.display_string.valuePlease notice that this property can be on both sides of an assignment operator. If it is on the right side, then we are reading the value, if it's of the left side, then we are assigning a new value. For example, to clear the string we can do this:
document.forms["calc_form"].elements["display_string"].value = "";to add character 'A' to the string we can use this:
document.calc_form.display_string.value += 'A';Now we are ready to write our add_character() function:
<script>
function add_character( ch )
{
document.calc_form.display_string.value += ch;
}
</script>
Handler for the duplicate button is practically the same, except we need first to read the value of the input string and then assign the new value, which contains the read value two times. It can be done by:
var cur_value = document.calc_form.display_string.value; document.calc_form.display_string.value = cur_value + cur_value;or shorter
document.calc_form.display_string.value += document.calc_form.display_string.value;
We practically discussed how to clear out the input string. All we need to do is to define a handler that will assign the value property of the input element the empty string. In the complete example we have all these small pieces of code assembled together. We also created two different styles for our buttons. Hope you'll figure it out by yourself.
| Code | Result | ||||
|---|---|---|---|---|---|
<style>
.menustyle {
background-color: #b1caf3;
cursor: default;
font-weight: bold;
}
</style>
...
<table cellpadding=2 cellspacing=0 border=1 class=menustyle>
<tr> <td> Yahoo </td> </tr>
<tr> <td> Google</td> </tr>
<tr> <td> Altavista </td> </tr>
<tr> <td> Lycos </td> </tr>
</table>
|
|
To change colors upon mouse movement we need to define event handlers for two events:
<img src='somefile.jpg' obClick='this.src="anotherfile.jpg"'>Thus, being used in <td> tag object this will refer to this particular cell.
Any displayable element on a page has sub-object style that contains information about style of the element. Addressing properties of the object style we can change the look of any element. We will use only two of these properties:
this.style.color = "blue";Here is a more complete example to illustrate how to combine these things together:
| Code | Result | ||
|---|---|---|---|
<table border=1> <tr> <td onMouseOver="this.style.color='red'">Cell one</td> <td onMouseOver="this.style.backgroundColor='yellow'">Cell two</td> </tr> </table> |
|
Remember that JavaScript is case sensitive. style.color and style.Color
will not work the same way.
To fulfill the last part of the objectives we need to jump to another page (just like a link allows us to do). We could've done that by using <a> tag inside the cells, but we'll use another way. We are going to use sub-object location of the object window. location object has only three properties:
window.location.href = "http://www.yahoo.com";Finally, to jump to a new page only when a user clicks we need to define handlers for the event Click for all cells in our menu:
<td ... onClick="window.location.href = 'http://www.yahoo.com';">
This file contains the complete example.
To create such an object we'll use tag <div> defined like this:
<div id=popup> </div>with the following style defined for it:
#popup {
visibility: hidden;
position: absolute;
...
width: 150px;
height: 200px;
}
The most important lines of the style are emphasized with the bold font. The first of them hides this object
on the page, so initially it's not visible. And the other makes its coordinate to absolute coordinates
on the page. To make this object appear we will use property visibility
of the sub-object style of this object, which, by the way, can be accessed by its ID as
document.all['popup'].style.visibility = "visible";and to make it disappear again:
document.all['popup'].style.visibility = "hidden";
To make our hint appear in the right place we can use two more properties of the style object:
document.all['popup'].style.posLeft = 100; document.all['popup'].style.posTop = 50;
Combining all of these together, to make our hint appear upon mouse movement over a particular object we need to define onMouseOver handler of this object like this:
<script>
fucntion show_hint(x, y)
{
document.all['popup'].style.posLeft = x;
document.all['popup'].style.posTop = y;
document.all['popup'].style.visibility = "visible";
}
</script>
...
...<td onClick="show_hint(115, 45);">I want a hint here</td>...
As you may notice this object does not contain any text in it. We'll take care of it in JavaScript code. To insert some text inside this pop-up object on-fly we can use property innerHTML, which contains all HTML text inside the object. For example, for the following object:
<div id=hint> This is an object with <b>ID</b>=<i>hint</i>. </div>this property ( which is accessible as
"This is an object with <b>ID</b>=<i>hint</i>."Using innerHTML property we can not only get to know what's inside the object under consideration, but also assign new content to it. For example, the following line clear out the content of the object hint from the previous example:
document.all['hint'].innerHTML = "";We can a little bit modify function show_hint() and make it take the third argument, text it should put inside the hint.
The very last problem we need to discuss here is how to make the hint disappear. Since the condition of disappearance is "when user moves the mouse out of the hint", then it seems to be very logical to define onMouseOut handler for the hint object:
<div id=popup onMouseOut="this.style.visibility='hidden';"> </div>
The following file contains the complete example. All coordinates in the example were chosen manually from experiments. Yes, there is a nicer way to locate the right position, but we decided not to put too many details in one example.
Please notice, that combining examples two and three together one can do rather difficult menus.
On the eBorCom Web Development Resources site you can find more details about JavaScript event handlers