Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   function function question from a newbie (http://www.velocityreviews.com/forums/t933072-function-function-question-from-a-newbie.html)

Harlett O'Dowd 10-02-2007 07:49 PM

function function question from a newbie
 
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.


David Golightly 10-02-2007 08:46 PM

Re: function function question from a newbie
 
On Oct 2, 12:49 pm, Harlett O'Dowd <chris.conne...@worldspan.com>
wrote:
> 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


Thomas 'PointedEars' Lahn 10-02-2007 11:21 PM

Re: function function question from a newbie
 
McKirahan wrote:
> Where is "Object" declared?


Implicitly by the implementation.

> Isn't "Object" a reserved word?!


It's not. But it is the identifier of a constructor of a built-in object.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee


All times are GMT. The time now is 05:11 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.