function function question from a newbie

H

Harlett O'Dowd

please forgive and be kind to this call for help.

I'm trying to script a web survey and I'm not having any luck.
Specifically, the survey is composed of eight questions (two will be
given in the sample below) and the possible responses are:

a) approve the measure
b) no opinion
c) object to the measure.

I am trying to create a function script whereby if the user chooses
(c) object, he had to elaborate on that by choosing one of four
possible flavors:

a) the project is not sound
b) the project duplicates a previous project
c) the project is not needed
d) other - which gives the user a text screen in which s/he can
explain his/her objections.

This is what I have scripted:

<SCRIPT>
function verify(){
f = document.survey;

if ( prop-one.radio value == Object || object-one.value == 0 ) {
alert('Please tell us why you object to Proposal #1, "Advanced
Metering Initiatives (AMI) Applications for Power Delivery".');
f.object-one.focus(); return false;
}

if ( prop-two.radio value == Object || object-two.value == 0 ) {
alert('Please tell us why you object to Proposal #1, "Advanced
Metering Initiatives (AMI) Applications for Power Delivery".');
f.prop-two.focus(); return false;
}

return true;
}

</SCRIPT>

anything immediately jump out and scream "idiot!" at you? (and yes,
"Object" is capitalized in the code - should it?)

Thanks in advance.
 
D

David Golightly

please forgive and be kind to this call for help.

I'm trying to script a web survey and I'm not having any luck.
Specifically, the survey is composed of eight questions (two will be
given in the sample below) and the possible responses are:

a) approve the measure
b) no opinion
c) object to the measure.

I am trying to create a function script whereby if the user chooses
(c) object, he had to elaborate on that by choosing one of four
possible flavors:

a) the project is not sound
b) the project duplicates a previous project
c) the project is not needed
d) other - which gives the user a text screen in which s/he can
explain his/her objections.

This is what I have scripted:

<SCRIPT>
function verify(){
f = document.survey;

if ( prop-one.radio value == Object || object-one.value == 0 ) {
alert('Please tell us why you object to Proposal #1, "Advanced
Metering Initiatives (AMI) Applications for Power Delivery".');
f.object-one.focus(); return false;
}

if ( prop-two.radio value == Object || object-two.value == 0 ) {
alert('Please tell us why you object to Proposal #1, "Advanced
Metering Initiatives (AMI) Applications for Power Delivery".');
f.prop-two.focus(); return false;
}

return true;
}

</SCRIPT>

anything immediately jump out and scream "idiot!" at you? (and yes,
"Object" is capitalized in the code - should it?)

Thanks in advance.

Have you attempted to execute this code? You should be seeing red
flags all over the place. Things to take care of right away:

1. Declare all variables using the "var" keyword.
2. Get your form objects using the document.forms collection. So for
example, the first line of your function should read "var f =
document.forms['survey']". This, assuming your HTML contains a <form>
tag with id="survey" or name="survey".
3. No identifiers (variable names) should use any characters other
than a-z, A-Z, 0-9, and _, and the first character should be either
alphabetic or an underscore.
4. To get at the elements in your form, use the form's elements
collection: f.elements['prop-one'] - the bracket syntax lets you have
form elements with IDs that contain non-valid identifier characters.
5. I'm not sure what this is even supposed to do:

if ( prop-one.radio value == Object || object-one.value == 0 ) {

Do you mean something like

if (f.elements['prop-one'].radio.value == 'Object' ||
f.elements['object-one'].value == '0')

perhaps?

6. Strings are strings when quoted, otherwise they're identifiers. If
you want to test against a string "Object", use 'Object', if you want
a variable called "Object", use Object.
7. Don't use variable names that overlap with builtin objects, like
Object.
8. Keep in mind that all form element values are always strings until
you convert them to something else, so treat them as such.
(JavaScript will perform type conversion automatically when using the
"==" operator, so ('0' == 0), but don't get in the habit of coding
like this, because when you try to add a number to a string, you'll
get a string, so ('0'+1 => '01'), not 1. This always trips up
beginners.)

Good luck!

-David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top