By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,628 Members | 1,880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,628 IT Pros & Developers. It's quick & easy.

referencing javascript form object

P: n/a
I have a javascript function as follows:

function myfunction(formName,formField) {
parseInt(document.formName.formField.value) +=1;
}

Then I can call the function when needed like this:

myfunction(users,username);

Here is my problem:

Javascript does not evaluate the arguments passed to the function.
Instead, it tries to use the arguments literarilly.

So instead of this:

parseInt(document.users.username.value) +=1;

javascript is actually looking for this:

parseInt(document.formName.formField.value) +=1;

I have also tried the following without success:

parseInt(eval(document.formName.formField).value) +=1;

Any suggestions on how to solve this problem?

Jul 23 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
phpcode wrote:
I have a javascript function as follows:

function myfunction(formName,formField) {
parseInt(document.formName.formField.value) +=1;
}

Then I can call the function when needed like this:

myfunction(users,username);

Here is my problem:

Javascript does not evaluate the arguments passed to the function.
Instead, it tries to use the arguments literarilly.

So instead of this:

parseInt(document.users.username.value) +=1;

javascript is actually looking for this:

parseInt(document.formName.formField.value) +=1;

I have also tried the following without success:

parseInt(eval(document.formName.formField).value) +=1;

Any suggestions on how to solve this problem?


You start by reading the group FAQ.

function myFunction(users,username){
parseInt(document.forms[users].elements[username].value,10) += 1;
}

Note that I added the radix of 10 to the parseInt call. And whether
parseInt is the best tool for the job depends on what the function is
attempting to do.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #2

P: n/a
Lee
phpcode said:

I have a javascript function as follows:

function myfunction(formName,formField) {
parseInt(document.formName.formField.value) +=1;
}

Then I can call the function when needed like this:

myfunction(users,username);

Here is my problem:

Javascript does not evaluate the arguments passed to the function.
Instead, it tries to use the arguments literarilly.

So instead of this:

parseInt(document.users.username.value) +=1;

javascript is actually looking for this:

parseInt(document.formName.formField.value) +=1;

I have also tried the following without success:

parseInt(eval(document.formName.formField).valu e) +=1;

Any suggestions on how to solve this problem?


Fix your function.
It should take a reference to a field as an argument.
Avoid using names when references are available.
function myfunction(formFieldReference) {
formFieldReference.value++;
}

and call it as:

myfunction(document.formName.formField);

or with some other reference to the field.

Jul 23 '05 #3

P: n/a
Randy,

Thanks for your answer.

However, you did not understand my question.

The form fieldnames are dynamically generated. I do not know them, and
cannot give the function the actual values. What I can do is pass it to
the function while calling the function.

I then want javascript to extract the real name from the arguments passed
to it.

In other words, I cannot do

function myfunction(users,username) {
}

because "users,username" changes each time the function is called.

What I can do is pass the new values to javascript at runtime.

Jul 23 '05 #4

P: n/a
Lee
phpcode said:

Randy,

Thanks for your answer.

However, you did not understand my question.

The form fieldnames are dynamically generated. I do not know them, and
cannot give the function the actual values. What I can do is pass it to
the function while calling the function.

I then want javascript to extract the real name from the arguments passed
to it.

In other words, I cannot do

function myfunction(users,username) {
}

because "users,username" changes each time the function is called.

What I can do is pass the new values to javascript at runtime.


That's what his example shows.

Jul 23 '05 #5

P: n/a
phpcode wrote:
Randy,

Thanks for your answer.

However, you did not understand my question.
OK.
The form fieldnames are dynamically generated. I do not know them, and
cannot give the function the actual values. What I can do is pass it to
the function while calling the function.
And then you use the function I posted. It takes the dynamic names as
parameters to the function.
I then want javascript to extract the real name from the arguments passed
to it.


How are you calling the function? If users and username is the text that
gets passed, like this:

function myFunction(formName,fieldName){
document.forms[formName].elements[fieldName].value++;
}

And call it like this:

myFunction('someFormName','someUserName')

Then the function will try to evaluate this:

document.forms['someFormName'].elements['someUserName'].value;

If that is not what you are trying to do, post some page code. Or, a URL.

And please read the FAQ with regards to quoting.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #6

P: n/a
In article
<ff******************************@localhost.talkab outprogramming.com>,
"phpcode" <qu*******@oasisoflove.com> wrote:
I have a javascript function as follows:

function myfunction(formName,formField) {
parseInt(document.formName.formField.value) +=1;
}

parseInt(document.formName.formField.value) +=1;
Causes an error in Netscape 7.1. parseInt returns a value.

In the following example, I show how a sting can be used to access a
form element. I put in some alerts to show the flow of the program and
how to debug problems.

Robert

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Simple validate</title>

<script type="text/javascript">

function myfunction(formName,formField) {
alert('formName = ' + formName +
' formField = ' + formField);

alert( document.forms[formName] )
alert( document.forms[formName].elements[formField] )
var newValue =
parseInt(document.forms[formName].elements[formField].value,10) +
1;
alert ('newValue = ' + newValue)
document.forms[formName].elements[formField].value = newValue;
}

</script>
</head>
<body>
<p>
The HTML file demonstrates how to validate a form field.</p>
<p>The form is submitted to the host when it doesn't contain an asterisk
(*).
</p>
<form name="myForm"
action="http://www.natAValidWebAddress.com"
method="POST"
onsubmit="alert('onsubmit...');myfunction('myForm' ,'theAddress');">

<p>Address:&nbsp;
<input type="text" name="theAddress" id='theAddress' size="40"></p>
<br>
<input type="submit" value="Submit address information">
</form>
</body>
</html>
Jul 23 '05 #7

P: n/a
I have tried all different combinations as suggested in this thread. Non
of them works.

Please try this demo page, and see the source:

http://www.homesillustratedabq.com/jstests.php

Jul 23 '05 #8

P: n/a
"phpcode" <qu*******@oasisoflove.com> wrote in message
news:c6******************************@localhost.ta lkaboutprogramming.com...
I have tried all different combinations as suggested in this thread. Non
of them works.

Please try this demo page, and see the source:

http://www.homesillustratedabq.com/jstests.php


"myfunc3()" was close but it appended not incremented.

Will this work for you? Watch for word-wrap.

<html>
<head>
<title>jstests.html</title>
<script>
function myfunc9(form,numb) {
var valu = parseInt(document.forms[form].elements[numb].value,10);
valu++;
document.forms[form].elements[numb].value = valu;
}
</script>
</head>
<body>
<form name="form9" style="margin:0">
Test9: &nbsp;
<input type="text" name="number" value="0">
<input type="button" value="click"
onClick="myfunc9('form9','number')">
</form>
</body>
</html>
Jul 23 '05 #9

P: n/a
Lee
phpcode said:

I have tried all different combinations as suggested in this thread. Non
of them works.

Please try this demo page, and see the source:

http://www.homesillustratedabq.com/jstests.php


None of those matches my suggestion. Here are two versions:

<html>
<head>
<title>demo</title>

<script type="text/javascript">

function myfunc1(field) {
field.value++;
}

function myfunc2(formName,fieldName) {
document.forms[formName].elements[fieldName].value++;
}

</script>
<body>

<form name="frank">
Test 1: <input name="iris" value="1">
<input type="button"
onclick="myfunc1(this.form.iris)" value="go">
<br>
Test 2: <input name="irene" value="2">
<input type="button"
onclick="myfunc2('frank','irene')" value="go">
</form>
</body>
</html>

Jul 23 '05 #10

P: n/a
If you think you will be programming in Javascript some more, you need
a good reference. Many folks in this group recommand:

javascript: The Definitive Guide by David Flanagan

Robert

Jul 23 '05 #11

P: n/a
instead of passing the name of the form as an argument, just use the
form itself. for example
if the name of the form is form1, then you can access it with
document.form1 right?
then
var formArg = document.form1;
myfunction(formArg);
and also formField in the same manner.
if you use this method in a form, perhaps as a response to the user's
clicking on a button, you could say
onclick="myfunction(this.form);"
that's how I work with forms. Hope this helps

Jul 23 '05 #12

P: n/a
I know this.

My problem had to do with doing math operations. I didn't have a problem
when values are strings.

Jul 23 '05 #13

P: n/a
I do have the O'Reilly book.

The problem I presented here had to do with incrementing an integer value,
not just a simple case of accessing a form field.

Jul 23 '05 #14

P: n/a
Thanks McKirahan. Your example worked.

Jul 23 '05 #15

P: n/a
Lee,

Thanks for your help.

Jul 23 '05 #16

P: n/a
"phpcode" <qu*******@oasisoflove.com> wrote in message
news:32******************************@localhost.ta lkaboutprogramming.com...
Thanks McKirahan. Your example worked.

How about this:

<html>
<head>
<title>jstests.html</title>
</head>
<body>
<form name="form9" style="margin:0">
Test9: &nbsp;
<input type="text" name="number" value="0">
<input type="button" value="click"
onClick="this.form.number.value++">
</form>
</body>
</html>
Jul 23 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.