passing variables

E

Ed

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){
entry=document.Forms[0].this.value;
if (document.Forms[0].this.value==""){
document.Forms[0].this.focus();
}
else {
// Do something with "entry" here
}
if (this = "inputBox2") {
document.Forms[0].total.value = document.Forms[0].inputBox1.value +
document.Forms[0].inputBox2.value;
}
}
</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>
 
G

Grant Wagner

Ed said:
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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 
E

Ely

I've played with your code for hours and keep getting "Expecting an Object"

Grant said:
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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top