473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

another document.[formName] has no properties error

apologies in advance, as not only am i new to learning how to code
javascript properly, i'm new to the groups posting thing...

i am developing in firefox 1.0+, but will be working in an msie 6.0+
produciton environment ( not my choice, but when is it ever? ).

the desired output is that when the end-user selects two radio buttons,
one from each 'group', the form / page will open an alert window
displaying the values of the radio buttons selected without having the
end-user left-click on a submit button.

i cannot figure out why I'm getting the "document.[formName] has no
properties" error mesasage in the javascript console.

any advice, assistance, comments or suggestions will be appreciated.

michael
this is what i've got thus far - - -

( located within the head tags )

<script language = "javascript" type = "text/javascript">
var myFirstVariable = "";
var mySecondVariable = "";

function optionSelected ()
{
for (i = 0; i < 4; i++);
{
if (document.[formName].myFirstVariable[i]checked == true);
{
firstVariableName = document.[formName].myFirstVariable.value;
}
}
for (i = 0; i < 1; i++);
{
if (document.[formName].mySecondVariable[i].checked == true);
{
seconVariableName = document.[formName].mySecondVariable.value;
}
}
alert("the radio buttons selected were + " + firstVariableName + " and
" + secondVariableName + ".");
}

</script>

....and then my form looks like...

( located within the body tags )

<form name = "formName">
<p>
<input type = "radio" name = "myFirstVariable" value = "option1"
onClick = "optionSelected()">Option1<br>
<input type = "radio" name = "myFirstVariable" value = "option2"
onClick = "optionSelected()">Option2<br>
<input type = "radio" name = "myFirstVariable" value = "option3"
onClick = "optionSelected()">Option3<br>
<input type = "radio" name = "myFirstVariable" value = "option4"
onClick = "optionSelected()">Option4<br>
<input type = "radio" name = "myFirstVariable" value = "option5"
onClick = "optionSelected()">Option5<br>
</p>
<p>
<input type = "radio" name = "mySecondVariable" value = "option1"
onClick = "optionSelected()">Option1<br>
<input type = "radio" name = "mySecondVariable" value = "option2"
onClick = "optionSelected()">Option2<br>
</p>
</form>

Oct 9 '05 #1
7 2928
michael wrote:
function optionSelected ()
{
for (i = 0; i < 4; i++);

The semicolon should be omitted here.
{
if (document.[formName].myFirstVariable[i]checked == true);
{


Again, the semicolon should be omitted here and the form field should be
referenced as follows:

document.forms[formName].elements['myFirstVariable'][i].checked
JW

Oct 9 '05 #2
michael wrote:
apologies in advance, as not only am i new to learning how to code
javascript properly, i'm new to the groups posting thing...

i am developing in firefox 1.0+, but will be working in an msie 6.0+
produciton environment ( not my choice, but when is it ever? ).

the desired output is that when the end-user selects two radio buttons,
one from each 'group', the form / page will open an alert window
displaying the values of the radio buttons selected without having the
end-user left-click on a submit button.

i cannot figure out why I'm getting the "document.[formName] has no
properties" error mesasage in the javascript console.

any advice, assistance, comments or suggestions will be appreciated.

michael
this is what i've got thus far - - -

( located within the head tags )

<script language = "javascript" type = "text/javascript">
The language attribute is depreciated, keep the text attribute. You
may find it easier to read your code if you don't leave spaces between
the attribute name, the '=' and the value:

<script type="text/javascript">

var myFirstVariable = "";
var mySecondVariable = "";
You have made two global variables that clash with the names you've
used in your forms. They may make life difficult, so either chose
better names or don't use them at all.

function optionSelected ()
{
for (i = 0; i < 4; i++);
If you declare variables inside a function without using 'var', the
variables become global. That can cause real problems with counters
in particular. Always keep variables local unless there is a really
good reason for them to be global.

for (var i=0; i<4; i++);

{
if (document.[formName].myFirstVariable[i]checked == true);


You have a couple of problems here. Firstly, you are using square
brackets incorrectly - they are used to access a property of an
object, they can't be used on their own. Read the FAQ:

<URL:http://www.jibbering.com/faq/#FAQ4_39>

Given that your form is called 'formName', you can reference it as:

document.formName;
or
document.forms['formName'];

The second method is preferred. You can generally do the same thing
with form controls:

document.formName.myFirstVariable;
or
document.forms['formName'].elements['myFirstVariable'];

or some combination of the two. Again, the second is preferred.

*But*, whenever there are a number of form controls with the same name
(like radio buttons) then you will get back a collection, not a single
element:

var rButtons = document.formName.myFirstVariable;

Now, to get the one that is checked:

var firstSelected;
for (var i=0, j=rButtons.length; i<j; i++){
if (rButtons[i].checked){
firstValue = rButtons[i].value;
}
}

Note that you don't have to compare (rButtons[i].checked == true)
because if it's checked, rButtons[i].checked will return true anyway
(or false if it's not).

Often the best way to get a reference to something that has been
clicked on is to use 'this', it give a reference directly to the
element without the hassle of hard-coded form or control names.
Here's some play code that should get you going:

<script type="text/javascript">

function showChecked(f)
{
var el;
var message = '';
var buttonNames = ['myFirstVariable','mySecondVariable'];

for (var i=0, j=buttonNames.length; i<j; i++){
el = f.elements[buttonNames[i]];
message += buttonNames[i] + ': ' + getValue(el) + '\n';
}
alert(message);
}

function getValue(r)
{
var i=r.length;
while (i--){
if (r[i].checked) return r[i].value;
}
return 'None selected';
}

</script>

<form name="formName" action="" onclick="showChecked(this);">
<p>
<input type="radio" name="myFirstVariable"
value="option1">Option1<br>
<input type="radio" name="myFirstVariable"
value="option2">Option2<br>
<input type="radio" name="myFirstVariable"
value="option3">Option3<br>
<input type="radio" name="myFirstVariable"
value="option4">Option4<br>
<input type="radio" name="myFirstVariable"
value="option5">Option5<br>
</p>
<p>
<input type="radio" name="mySecondVariable"
value="option1">Option1<br>
<input type="radio" name="mySecondVariable"
value="option2">Option2<br>
</p>
</form>
Incidentally, at least one radio button should always be selected.

[...]
--
Rob
Oct 9 '05 #3
Janwillem,

thanks for the heads up.

remeber reading that there's supposed to be a semicolon at the end of
each sentence.

i see how i was making some assumptions in my coding in trying to
retrieve the values selected from the form.

michael

Oct 10 '05 #4
Rob,

thanks for the example. now i'm just trying to get it working with the
real stuff.

a couple questions if i may, as i'm just wanting to get a better
understanding of the thought process -

1. what does the f in the showChecked (f) function represent? ( same
would be asked about the variable el within the same function and the r
in getValue (r) function)

2. how would you suggest working in an "if" so that the script wouldn't
run unless a radio button from each group were selected? ( i know the
script would be run for both selections, i mean to not show the alert
until both have been selected )

michael

Oct 10 '05 #5
On 10/10/2005 17:55, michael wrote:

[snip]
remeber reading that there's supposed to be a semicolon at the end of
each sentence.


It is good practice to include a semicolon at the end of most
statements, but control structures are generally an exception; they
should be followed by blocks.

myFunction(...);
myVariable = ...;
return myValue;

but

if(...) {...}
while(...) {...}

[snip]

Mike
Please quote /relevant/ material when replying to posts in this group
(as demonstrated above). If you have to use Google Groups, use the 'show
options' link and select 'Reply' from the revealed panel (not the later
link).

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Oct 10 '05 #6
michael said the following on 10/10/2005 12:55 PM:
Janwillem,
This is Usenet, not email. Quote what you are replying to please. It
makes the conversation easier to follow.
thanks for the heads up.

remeber reading that there's supposed to be a semicolon at the end of
each sentence.
"Supposed to be"? No.
Recommended? Yes, in some places.
But, I have yet to see a script that will properly execute with a ; at
the end but won't execute without it.
i see how i was making some assumptions in my coding in trying to
retrieve the values selected from the form.


Ain't scripting fun?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?
Oct 10 '05 #7
michael said the following on 10/10/2005 1:06 PM:
Rob,

thanks for the example. now i'm just trying to get it working with the
real stuff.


Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 10 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Miles Davenport | last post by:
My Javascript is rather rusty :( ... and I need to do change some form values, in the folowing way: (1). I have the following a href (wrapped in PHP), which calls processForm. ==== <input...
5
by: Jeff | last post by:
Hi, I am trying to set the value of one drop down select box to the value of another drop down select box. I have the following in a function. document.formname.boxto.options.v alue =...
3
by: Don | last post by:
I can successfully force submission of a <form> using "document.formname.submit()". But, the submission doesn't appear to occur when used with Netscape. Anybody know why this is happening, and...
6
by: skubik | last post by:
Hi everyone. I'm attempting to write a Javascript that will create a form within a brand-new document in a specific frame of a frameset. The problem is that I can create the form and input...
2
by: Scott | last post by:
I need to write a function that copies variables to fields. I've used an array and loop because it's neater than writing a similar sentence out 10 times. var myString = new...
4
by: Bugs | last post by:
Hi, I wonder if anyone can help me out. I'm building a vb.net application that has a form with a panel that contains several other sub forms (as a collection of controls). What I'm wanting to...
8
by: Geoff Cox | last post by:
Hello, When using Internet Explorer, on networked PCs in a college, to view a page of mine with Javascript code in it the "stack overflow" error message appears. When I access the same file...
3
by: droesler | last post by:
I have a function that receives a form name and field name. someFunc(formName, fieldName) { var fieldVal = document.forms.elements.value; } This triggers the 'has no properties error. An...
23
by: Stanimir Stamenkov | last post by:
<form name="myForm" action="..."> <p><input type="text" name="myElem"></p> </form> As far as I was able to get the following is the standard way of accessing HTML form elements: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.