User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 374,006 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,838 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 456 | Replies: 4
Reply
Join Date: Jun 2005
Posts: 85
Reputation: michael123 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
michael123 michael123 is offline Offline
Junior Poster in Training

Question javascript variable issue

  #1  
Mar 6th, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2008
Location: Bangalore, India
Posts: 327
Reputation: DangerDev is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 31
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Whiz

Re: javascript variable issue

  #2  
Mar 7th, 2008
var fd="field_0";
here fd is a variable having value "field_0"

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 !!!
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
~Mitch Ratcliffe
Reply With Quote  
Join Date: Jun 2005
Posts: 85
Reputation: michael123 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
michael123 michael123 is offline Offline
Junior Poster in Training

Re: javascript variable issue

  #3  
Mar 7th, 2008
thanks, that help me.
Reply With Quote  
Join Date: Jun 2005
Posts: 85
Reputation: michael123 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
michael123 michael123 is offline Offline
Junior Poster in Training

Re: javascript variable issue

  #4  
Mar 7th, 2008
Another issue:
getElementById() works only in IE, if I try to use Firefox this will not work, how to solve this issue?
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,731
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 323
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: javascript variable issue

  #5  
Mar 7th, 2008
> 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.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 10:32 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC