www.webdeveloper.com
Results 1 to 10 of 10

Thread: Need help with javascript code

  1. #1
    Join Date
    Apr 2009
    Posts
    4

    Need help with javascript code

    Hey guys,
    I'm new with Javascript, so please excuse my ignorance. I need a javascript which will display the user's choice in an alert text box:

    <FORM NAME="testform" ACTION="" METHOD="GET">
    What kind of animal is your favorite pet? <BR>
    <INPUT TYPE="radio" NAME="pet" Value="Dog">Dog<BR>
    <INPUT TYPE="radio" NAME="pet" Value="Cat">Cat<BR>
    <INPUT TYPE="radio" NAME="pet" Value="Bird">Bird<BR>
    <INPUT TYPE="radio" NAME="pet" Value="Fish">Fish<BR>
    <INPUT TYPE="radio" NAME="pet" Value="Horse">Horse<BR>
    <INPUT type="button" value="Submit"
    onclick="valForm(myform); return false;">
    </FORM>

    Can anyone help me?

  2. #2
    Join Date
    Apr 2009
    Posts
    33
    Where and when?

    ps. the command is "alert(value)"

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,330

    Lightbulb

    Alert() won't execute the <input> tags you have specified.

    Try wrapping the form inside a <div> tag instead with the CSS style="display:none".
    Then when required, change the style to "display:block" which should 'pop-up' the tag with the contents visible.

  4. #4
    Join Date
    Apr 2009
    Posts
    4
    I've been playing with the code some. Here is my Javascript with the html:
    <HTML>
    <BODY>
    <SCRIPT LANGUAGE="JavaScript">
    var myOption = false
    function initValue() {
    myOption = document.forms[0].pet[3].checked
    }
    function pet(form) {
    for (var i = 0; i < form.pet.length; i++) {
    if (form.pet[i].checked) {
    break
    }
    }
    alert("You chose " + form.pet[i].value + ".")
    }

    </SCRIPT>

    <BODY>
    <FORM NAME="testform" ACTION="" METHOD="GET">
    What kind of animal is your favorite pet? <BR>
    <INPUT TYPE="radio" NAME="pet" Value="Dog" onClick="setShemp(false)">Dog<BR>
    <INPUT TYPE="radio" NAME="pet" Value="Cat" onClick="setShemp(false)">Cat<BR>
    <INPUT TYPE="radio" NAME="pet" Value="Bird" onClick="setShemp(false)">Bird<BR>
    <INPUT TYPE="radio" NAME="pet" Value="Fish" onClick="setShemp(false)">Fish<BR>
    <INPUT TYPE="radio" NAME="pet" Value="Horse" onClick="setShemp(true)">Horse<BR>
    <INPUT TYPE="button" NAME="Submit" VALUE="Submit" onClick="fullName(this.form)">
    </FORM>
    </BODY>
    </HTML>

    What am I doing wrong?

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,330

    Lightbulb

    In the following:
    Code:
    function pet(form) {
      for (var i = 0; i < form.pet.length; i++) {
        if (form.pet[i].checked) { break }
      }
      alert("You chose " + form.pet[i].value + ".")
    }
    Save the loop 'i' value in another variable as it is not recognize outside the loop.
    Try:
    Code:
    function pet(form) {
      var flag = -1;
      for (var i = 0; i < form.pet.length; i++) {
        if (form.pet[i].checked) { flag = i;  break; }
      }
      if (flag < 0) { alert('Nothing checked!'); } else { alert("You chose " + form.pet[flag].value + "."); }
    }

  6. #6
    Join Date
    Apr 2009
    Posts
    4
    I appreciate the advice, but that doesn't seem to be working. In fact, you click on a selection and nothing happens, no alert box, nothing.

    Any ideas?

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,330

    Question Confused ...

    Where is your function "setShemp()" in post #1?

    Where do you call the function "pet(form)"? Don't expect an alert if the function is never called!

    And what is "fullName(this.form)" supposed to do?

    When do you initialize using "initValue()"?
    And why not just
    <input type="radio" name="ped" value="Fish" onClick="setShemp(true)" checked>
    instead of the function?

    Also, "language='JavaScript'" is depricated so you should use:
    <script type="text/javascript">
    instead.

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,330

    Lightbulb

    Sure do have a lot of errors in original, but the following works:
    Code:
    <HTML>
    <BODY>
    <SCRIPT type="text/javascript" LANGUAGE="JavaScript">
    /*  following not used anywhere ???
    var myOption = false
    function initValue() {
      myOption = document.forms[0].pets[3].checked;
    }
    */
    function pet() {
      for (var i = 0; i < document.testform.pets.length; i++) {
        if (document.testform.pets[i].checked) { break }
      }
      alert("You chose " + document.testform.pets[i].value + ".")
    }
    
    </SCRIPT>
    
    <BODY>
    <FORM NAME="testform" ACTION="" METHOD="GET" onsubmit="pet();return false">
    What kind of animal is your favorite pet? <BR>
    <INPUT TYPE="radio" NAME="pets" Value="Dog">Dog<BR>
    <INPUT TYPE="radio" NAME="pets" Value="Cat">Cat<BR>
    <INPUT TYPE="radio" NAME="pets" Value="Bird">Bird<BR>
    <INPUT TYPE="radio" NAME="pets" Value="Fish">Fish<BR>
    <INPUT TYPE="radio" NAME="pets" Value="Horse">Horse<BR>
    <INPUT TYPE="submit" VALUE="Submit">
    </FORM>
    </BODY>
    </HTML>
    If my assumptions as to what you are trying to do are correct.

  9. #9
    Join Date
    Apr 2009
    Posts
    4

    Smile

    That worked! Thanks
    I guess I need to update my code. Also, I was trying so many things, that I forgot to go back and define (or undefine).
    Thnaks again.

  10. #10
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,330
    You're welcome.
    Glad I was able to help (or interpret what you wanted)
    Good Luck!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles