Connecting Tech Pros Worldwide Forums | Help | Site Map

referencing javascript form object

phpcode
Guest
 
Posts: n/a
#1: Jul 23 '05
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?


Randy Webb
Guest
 
Posts: n/a
#2: Jul 23 '05

re: referencing javascript form object


phpcode wrote:[color=blue]
> 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?
>[/color]

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
Lee
Guest
 
Posts: n/a
#3: Jul 23 '05

re: referencing javascript form object


phpcode said:[color=blue]
>
>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?[/color]

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.

phpcode
Guest
 
Posts: n/a
#4: Jul 23 '05

re: referencing javascript form object


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.

Lee
Guest
 
Posts: n/a
#5: Jul 23 '05

re: referencing javascript form object


phpcode said:[color=blue]
>
>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.[/color]

That's what his example shows.

Randy Webb
Guest
 
Posts: n/a
#6: Jul 23 '05

re: referencing javascript form object


phpcode wrote:[color=blue]
> Randy,
>
> Thanks for your answer.
>
> However, you did not understand my question.[/color]

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

And then you use the function I posted. It takes the dynamic names as
parameters to the function.
[color=blue]
> I then want javascript to extract the real name from the arguments passed
> to it.[/color]

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
Robert
Guest
 
Posts: n/a
#7: Jul 23 '05

re: referencing javascript form object


In article
<ff64e82a3acc9ba32aee7eff45f5bf10@localhost.talkab outprogramming.com>,
"phpcode" <questions@oasisoflove.com> wrote:
[color=blue]
> I have a javascript function as follows:
>
> function myfunction(formName,formField) {
> parseInt(document.formName.formField.value) +=1;
> }[/color]


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>
phpcode
Guest
 
Posts: n/a
#8: Jul 23 '05

re: referencing javascript form object


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

McKirahan
Guest
 
Posts: n/a
#9: Jul 23 '05

re: referencing javascript form object


"phpcode" <questions@oasisoflove.com> wrote in message
news:c6d0e6dbee9235b446dfb6d2ba19eaec@localhost.ta lkaboutprogramming.com...[color=blue]
> 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
>[/color]

"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>


Lee
Guest
 
Posts: n/a
#10: Jul 23 '05

re: referencing javascript form object


phpcode said:[color=blue]
>
>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
>[/color]

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>

Robert
Guest
 
Posts: n/a
#11: Jul 23 '05

re: referencing javascript form object


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

fancyydk
Guest
 
Posts: n/a
#12: Jul 23 '05

re: referencing javascript form object


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

phpcode
Guest
 
Posts: n/a
#13: Jul 23 '05

re: referencing javascript form object


I know this.

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

phpcode
Guest
 
Posts: n/a
#14: Jul 23 '05

re: referencing javascript form object


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.

phpcode
Guest
 
Posts: n/a
#15: Jul 23 '05

re: referencing javascript form object


Thanks McKirahan. Your example worked.



phpcode
Guest
 
Posts: n/a
#16: Jul 23 '05

re: referencing javascript form object


Lee,

Thanks for your help.

McKirahan
Guest
 
Posts: n/a
#17: Jul 23 '05

re: referencing javascript form object


"phpcode" <questions@oasisoflove.com> wrote in message
news:322c1f3a8c02c0ba04d5a636d76ee7dd@localhost.ta lkaboutprogramming.com...[color=blue]
> Thanks McKirahan. Your example worked.[/color]


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>


Closed Thread


Similar JavaScript / Ajax / DHTML bytes