WISDOMBAY

in4mation ... organized

Master Javascript by Typing PART II

Page 3


Launch your Text Editor, type the above given code & save as fifth.html (or any other name you prefer). Now view the file that you created in browser & you will see 2 text boxes were you can type the desired forecolour & backcolour for your page. Press the Set Colour button to see the desired effect.



Analysing the code

Here we are calling a function called set() on the click event for the Set Colour Button. In this function we are reading the values typed by user in the textboxes in to 2 variables called uf & bf.
For reading the value of a textbox we are using the following code format.

document.all.formname.inputtypename.value
.

i.e. in order to get the value typed in a textbox named 'fc', which is placed in a form called 'cll' use,

document.all.cll.fc.value

The values are then passed to another function called setColor(), which has a signature like function setColor(forecolor,backcolor). We are calling this function in our code like setColor(uf,bf). The value that is in the variable uf will be placed to forecolor variable & value in bf variable will be placed in backcolor variable. So what actually we are doing here is using arguments or parameters with our functions.

The setColor(forecolor,backcolor) function uses 2 statements to set the foreground & background colour of a page.

The document.body.text object is used to set text color of a page. Here we are setting the first value that is passed to our function as the text or foreground color.

document.body.text = forecolor
The document.body.bgColor object is used to set background color of a page. Here we are setting the second value that is passed to our function as the background color.

Note : The 'C' in bgColor is Capital. Use it like that, else it won't work.

document.body.bgColor = backcolor

That's it guys/gals, we are concluding our second edition of this paper on javascripting here. Will be back next month with more practical examples.

Google