473,323 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

passing variables

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>

Jul 20 '05 #1
2 27239
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
Jul 20 '05 #2
Ely
I've played with your code for hours and keep getting "Expecting an Object"

Grant Wagner wrote:

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

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
4
by: Jason Us | last post by:
Does anyone have experience with passing variables from an ASP page to a JSP page. The way it currently works in passing the SSN in the URL. This cannot be good. I thought that storing a...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: Jack | last post by:
Hi, I need to pass multple variables in a link in order to go to a asp page with the two varables. The following are the values of the variables using response.write: <%'Response.Write Mypage...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
3
by: Lee | last post by:
Hi All How can I pass options from one webpage into another webpage. When the user clicks on the hyperlink I want them to be go to the next page but I need to pass in a number that the next...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
6
BezerkRogue
by: BezerkRogue | last post by:
This is the most fundamental action I am sure, but I can't seem to make it happen. I am familiar with passing variables in ASP. But that doesn't seem to be the preferred method in .NET. I have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.