Google Answers Logo
View Question
Q: Need 2 short HTML/Javascript pieces of code ( Answered 5 out of 5 stars,   0 Comments )
Subject: Need 2 short HTML/Javascript pieces of code
Category: Computers > Programming
Asked by: amirehsans-ga
List Price: $6.00
Posted: 19 May 2003 16:51 PDT
Expires: 18 Jun 2003 16:51 PDT
Question ID: 206061
Hi. I have three short questions:

1. In HTML, how do you call a function when a text is clicked on? (i
know how to call a function when a button is pressed, but not when a
text is clicked on)

2. what is the code for a HTML/Javascript function that makes a given
URL, "...", that browser's homepage?
(yes, I would appreciate it if it also opens up a dialogue box asking
for confirmation before proceeding)

3. Let's say I have two drop-down menus called A and B. I would like a
third component, a textbox, named C, to display a number that is equal
(the "value" of A times 50) + (the "value" of B times 14).

Also, I want a label called D to display the following text:
"Total cost for X months and Y weeks is:"

where X is the "value" of A and Y is the "value" of B.

NOTE: the default value of A & B is 0, so you don't need to worry
about the default case.

Could you please provide the Javascript code that does the calculation
and output for C and the text output for D.

Thank you very much!
Subject: Re: Need 2 short HTML/Javascript pieces of code
Answered By: errol-ga on 19 May 2003 19:37 PDT
Rated:5 out of 5 stars
Hi there, Amirehsans!

I have a fair amount of Javascript/HTML experience and I'll answer
these in the original order with notes under each example.
You'll have to watch out for word wrapping in these code examples,
especially No 3.




<script type="text/javascript">
function dofunction()
	alert("Alert Box")

<a href="nonscriptedpage.html" onclick="dofunction(); return
false;">Click to call event</a>


In the <head> we have a standard function which is then called when
the link is clicked on using the Onclick event handler.
Other event handlers include OnMouseUp, OnMouseDown, OnMouseOver and
The "nonscriptedpage.html" part is for any browsers which have
Javascript disabled, the "return false;" statement will stop

the actual "href" part from executing on Javascript enabled browsers.
For more, see here:




<!--[if IE]>
<a href="#" onclick="'url(#default#homepage)';
this.setHomePage('');">Set as homepage!</a>


This short code will only work in Internet Explorer 5+ and will make
the standard "Set as Homepage?" dialog box appear.




<script type="text/javascript">

	function calculate() {

// Textbox D which is A + B

		document.calcscript.d.value = ab

// Textbox C which is A * 50 + B * 14

		a50=document.calcscript.a.value * 50 // Change these parts
		b14=document.calcscript.b.value * 14 // for different
		z = a50 + b14
		document.calcscript.c.value = z
<div align="center">
<form name="calcscript">
	<select name="a">
	<select name="b">
	<input type="text" size="10" name="c">

	Total cost for X months and Y weeks is: <input type="text" size="10"
	<input type="button" value="Calculate" onClick="calculate()">


I included some notes in the script itself.
As you can see, the first part basically says:
"X is the option you select in the form object A, same goes for B.
Then, AB is A + B. Lastly, make the forum object D have

the value of AB."
Simply copy and paste the lot into a text editor, save as a .html file
then open it in your browser to test.


It was a pleasure to code these for you.
I don't have anything in the way of links or searches unfortunately
because I knew how to code these but if you need any clarification,
don't hesitate to ask.

Kind regards,

Request for Answer Clarification by amirehsans-ga on 20 May 2003 13:48 PDT
Thanks for your great answer. 

I implemented your code and realized that it outputs the answer to the
Total cost in a form like this:

"Total cost for X months and Y weeks is:" + A Text Box 

But I wanted to use a label instead, that is, I want the calculated
answer seem to be part of the sentence, something like:

"Total cost for X months and Y weeks is: 50" 

So I guess you could help me in either of 2 ways:
1. Show me how to display the answer in a label as opposed to a
2. show me how to disable a text box so that no one can change it's

Thanks very much.

Also, I want a label called D to display the following text: 
"Total cost for X months and Y weeks is:"

Request for Answer Clarification by amirehsans-ga on 20 May 2003 13:52 PDT
The last two lines of my last comment are just quoted from the
original question. :->

Clarification of Answer by errol-ga on 20 May 2003 15:39 PDT
Hello again!

If I'm reading it correctly, you wanted it to be a label so nobody can
alter it's value.
Well, altering text like this in real time is a lot more complicated
but the simple solution is to make box D read only like this:

Total cost for X months and Y weeks is: <input type="text" size="10"
name="d" readonly="readonly">

amirehsans-ga rated this answer:5 out of 5 stars
You really did a great job errol! Thank U :-p

There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy