Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-14-2004, 08:44 PM   PM User | #1
Tom Armstrong
New to the CF scene

 
Join Date: May 2004
Location: Worcester, MA
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Tom Armstrong is an unknown quantity at this point
Script doesn't see form control

One problem I'm having is that The script in this HTML doesn't see the control called "gender" and always read's its value as "undefined". A previous posting which is gone now had some good advice.

The other problem is that I had a thread started here and I'm not sure if I put it in the wrong forum or what but it's gone. So PLEASE, if this posting violates a rule let me know, I'm new at this!



<HTML>
<BODY>

<FORM>

<INPUT TYPE="TEXT" NAME="actualValue" SIZE=40> should be "M" or "F" </INPUT><BR><P>

<INPUT TYPE="radio" NAME="gender" value="M" checked>Male<BR>
<INPUT TYPE="radio" NAME="gender" value="W">Female<BR><BR>

<INPUT TYPE="button" NAME="myButt" value = "Click to see radio button value" onClick=myFunction()></INPUT>

</BODY>
</FORM>

<SCRIPT>


function myFunction()
{
document.forms[0].actualValue.value = document.forms[0].gender.value;

// should be "M" or "F" but it's "undefined"
}

</SCRIPT>
</HTML>
Tom Armstrong is offline   Reply With Quote
Old 05-14-2004, 11:06 PM   PM User | #2
sad69
Senior Coder

 
Join Date: Feb 2004
Posts: 1,206
Thanks: 0
Thanked 0 Times in 0 Posts
sad69 is an unknown quantity at this point
Well the reason it's undefined is because you've got TWO elements named 'gender'. So how does JS know which one you're talking about?

Because you've got two elements name gender, you've now got a gender array. Try this function instead of what you're using and see how it works out for you:
Code:
function myFunction() {
 for(var i = 0; i < document.forms[0].gender.length; i++) {
  if(document.forms[0].gender[i].checked)
   document.forms[0].actualValue.value = document.forms[0].gender[i].value;
 }
}
Let me know if you have any further questions/comments.

Sadiq.
sad69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:56 PM.


Advertisement
Log in to turn off these ads.