Ed wrote:
I need to know how to pass variables because I want to use the same
function to varify multiple data imput boxes.
Please tell me what's wrong with this code.
<head>
<script language="JavaScript">
function myFunction(this){
"this" is a keyword representing the current object, it shouldn't and can't
be used as a parameter name.
Try
function myFunction(referenceToAnInput) {
entry=document.Forms[0].this.value;
You've got a reference to the form element itself, it's unnecessary to
reference the input this way. Simply use:
var entry = referenceToAnInput.value;
if (document.Forms[0].this.value==""){
You store the input's value in a variable then you don't use it. If you're
retrieving "entry", then make use of it:
if (entry == "") {
document.Forms[0].this.focus();
referenceToAnInput.focus();
}
else {
// Do something with "entry" here
}
if (this = "inputBox2") {
if (referenceToAnInput.name == "inputBox2") {
document.Forms[0].total.value = document.Forms[0].inputBox1.value
+document.Forms[0].inputBox2.value;
var referenceToTheForm = referenceToAnInput.form;
referenceToTheForm.total.value =
parseFloat(referenceToTheForm.inputBox1.value) + parseFloat(entry);
You need to parseFloat() because the values in input boxes are strings, if
you simply added them together with "+", it would concatenate them, not add
their numeric values (see <url: http://jibbering.com/faq/#FAQ4_21 />)
Since you retrieve "entry" at the beginning of the function, you might as
well use it here rather then retrieving it again.
}
}
</script>
</head>
<body>
<form>
<input type="text" name="inputBox1" onBlur="myFunction(this)">
<input type="text" name="inputBox2" onBlur="myFunction(this)">
<input type="text" name="total">
</form>
</body>
Note that by using the onblur event, and putting focus back on the input
when the input is invalid, you've trapped the user in that input until they
enter something, and created a situation that could lead to an endless loop
where they blur the input, you put the focus back on it, some other sequence
of events blurs it again and so on.
--
| Grant Wagner <gw*****@agricoreunited.com>
* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html
* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp
* Netscape 6/7 DOM Reference available at:
*
http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
*
http://www.mozilla.org/docs/web-deve...upgrade_2.html