function function question from a newbie

Discussion in 'Javascript' started by Harlett O'Dowd, Oct 2, 2007.

  1. 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.
     
    Harlett O'Dowd, Oct 2, 2007
    #1
    1. Advertising

  2. On Oct 2, 12:49 pm, Harlett O'Dowd <>
    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
     
    David Golightly, Oct 2, 2007
    #2
    1. Advertising

  3. 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
     
    Thomas 'PointedEars' Lahn, Oct 3, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry C.
    Replies:
    8
    Views:
    279
    Uri Guttman
    Nov 23, 2003
  2. Kruno Saho
    Replies:
    0
    Views:
    169
    Kruno Saho
    Apr 7, 2013
  3. Dave Angel
    Replies:
    0
    Views:
    147
    Dave Angel
    Apr 7, 2013
  4. rusi
    Replies:
    0
    Views:
    133
  5. Miki Tebeka
    Replies:
    0
    Views:
    95
    Miki Tebeka
    Apr 7, 2013
Loading...

Share This Page