943,781 Members | Top Members by Rank

Ad:
Mar 6th, 2008
0

javascript variable issue

Expand Post »
I have a form text field called "field_0", and javascript code:
<script language="javascript">
function test() {
var a=document.form1.field_0.value;
alert (a);
}
</script>
<form name=form1 ....>
<input type=text name=field_0>
<input type=text name=field_1>
<....onclick='javascript: test();'>
</form>

if I run this, it pops up correct message box with the "field_0" value.

However if I change a little in javascript code:
<script language="javascript">
function test() {
var fd="field_0";
var a=document.form1.fd.value;
alert (a);
}
</script>
this script will no longer work, how can I use variable "fd" in "document.form...."? any idea?

Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
michael123 is offline Offline
93 posts
since Jun 2005
Mar 7th, 2008
0

Re: javascript variable issue

Quote ...
var fd="field_0";
here fd is a variable having value "field_0"

Quote ...
var a=document.form1.field_0.value;
here field_0 is an object inside form1.

so you cant do what you want to do ?
any way why actually you want this to be done ?
you can try alternatives like getElementById();
hope this helps !!!
Reputation Points: 165
Solved Threads: 59
Posting Pro in Training
DangerDev is offline Offline
485 posts
since Jan 2008
Mar 7th, 2008
0

Re: javascript variable issue

thanks, that help me.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
michael123 is offline Offline
93 posts
since Jun 2005
Mar 7th, 2008
0

Re: javascript variable issue

Another issue:
getElementById() works only in IE, if I try to use Firefox this will not work, how to solve this issue?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
michael123 is offline Offline
93 posts
since Jun 2005
Mar 7th, 2008
0

Re: javascript variable issue

> I have a form text field called "field_0", and javascript code:

First of all, your markup is broken. The HTML tag attributes need to be in the form of key-value pairs with the value being enclosed in double quotes.

> However if I change a little in javascript code this script will no longer work

This is because your JS code now looks for an element with the name 'fd' instead of 'field_0'. Try something like document.form1[fd].value .

But the way you are accessing forms is fundamentally broken, read this excellent article on form access.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Help with Ajax toolkit drop downs
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Using ASCII codes??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC