javascript variable issue

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jun 2005
Posts: 92
Reputation: michael123 is an unknown quantity at this point 
Solved Threads: 0
michael123 michael123 is offline Offline
Junior Poster in Training

javascript variable issue

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 59
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: javascript variable issue

 
0
  #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 !!!
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 92
Reputation: michael123 is an unknown quantity at this point 
Solved Threads: 0
michael123 michael123 is offline Offline
Junior Poster in Training

Re: javascript variable issue

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

Re: javascript variable issue

 
0
  #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 Quick reply to this message  
Join Date: Jun 2006
Posts: 7,653
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: javascript variable issue

 
0
  #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.
The romantic image of an über-programmer is someone who fires up Emacs, types like a machine gun, and delivers a flawless final product from scratch. A more accurate image would be someone who stares quietly into space for a few minutes and then says “Hmm. I think I’ve seen something like this before.” - John D
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



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


Views: 843 | Replies: 4
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC