Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 20th, 2005, 10:21 AM
Ed
Guest
 
Posts: n/a
Default passing variables

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>

  #2  
Old July 20th, 2005, 10:21 AM
Grant Wagner
Guest
 
Posts: n/a
Default Re: passing variables

Ed wrote:
[color=blue]
> 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){[/color]

"this" is a keyword representing the current object, it shouldn't and can't
be used as a parameter name.

Try

function myFunction(referenceToAnInput) {
[color=blue]
> entry=document.Forms[0].this.value;[/color]

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;
[color=blue]
> if (document.Forms[0].this.value==""){[/color]

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 == "") {
[color=blue]
> document.Forms[0].this.focus();[/color]

referenceToAnInput.focus();
[color=blue]
> }
> else {
> // Do something with "entry" here
> }
> if (this = "inputBox2") {[/color]

if (referenceToAnInput.name == "inputBox2") {
[color=blue]
> document.Forms[0].total.value = document.Forms[0].inputBox1.value
> +document.Forms[0].inputBox2.value;[/color]

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.
[color=blue]
> }
> }
> </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>[/color]

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 <gwagner@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


  #3  
Old July 20th, 2005, 10:21 AM
Ely
Guest
 
Posts: n/a
Default Re: passing variables

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

Grant Wagner wrote:[color=blue]
>
> Ed wrote:
>[color=green]
> > 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){[/color]
>
> "this" is a keyword representing the current object, it shouldn't and can't
> be used as a parameter name.
>
> Try
>
> function myFunction(referenceToAnInput) {
>[color=green]
> > entry=document.Forms[0].this.value;[/color]
>
> 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;
>[color=green]
> > if (document.Forms[0].this.value==""){[/color]
>
> 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 == "") {
>[color=green]
> > document.Forms[0].this.focus();[/color]
>
> referenceToAnInput.focus();
>[color=green]
> > }
> > else {
> > // Do something with "entry" here
> > }
> > if (this = "inputBox2") {[/color]
>
> if (referenceToAnInput.name == "inputBox2") {
>[color=green]
> > document.Forms[0].total.value = document.Forms[0].inputBox1.value
> > +document.Forms[0].inputBox2.value;[/color]
>
> 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.
>[color=green]
> > }
> > }
> > </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>[/color]
>
> 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 <gwagner@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[/color]
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles