www.webdeveloper.com
Results 1 to 4 of 4

Thread: Please help with basic javascript question...

  1. #1
    Join Date
    Apr 2011
    Posts
    8

    Please help with basic javascript question...

    Hi all,

    After much search finally found this on a forum:

    Question:

    "I have 4 checkboxes (named box1, box2 , box3 and box4) and I need to send
    the user to one page if only boxes 1 and 2 are checked and to another page
    if any other combinations are checked."

    Answer:

    "if (theForm.box1.checked && theForm.box2.checked && !theForm.box3.checked &&
    !theForm.box4.checked)
    window.location.href = "Page1.htm"
    else
    window.location.href = "Page2.htm""

    ****

    Now my question: How should the full, working javascript look? (I mean the whole thing starting with <script language="JavaScript"><!-- and ending with //--></script>) ?

    How do I call the script in form tag, onsubmit?

    Many thanks...

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,364

    Lightbulb

    Nice question.

    Now post an attempt at the answer.
    We have too much to do beside doing your homework.

    Note for modern browsers, you do not need the:
    Code:
    // language = "JavaScript" // archaic 
    // use this instead
    <script type="text/javascript">
    Also the <!-- and --> within the script are not needed either.
    Comments can be included by either
    // comment
    or
    /* many
    lines
    of
    comments
    */

  3. #3
    Join Date
    Apr 2011
    Posts
    8
    JMRKER, thanks for the answer. It is not for my homework, believe it or not for 7 years now I make a living from web publishing. Until now, I just focused on writing content; these are my first steps at actual web development...

    I tried to use the script in a JavaScript external file that validates a form. I just pasted it below function validate(form) {

    Obviously, it did not work...

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,364

    Question

    Show the script and HTML, or a link to a live site, so we can see what you are doing.
    It does not need to be your entire site, just a sample of the problem you are having with your post.
    Difficult to comment on a snippet of code. Any suggestions I might make at this point would be just a SWAG.

    Something as simple as this for testing purposes:
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <meta charset="utf-8" />
    <title> Untitled </title>
    <script type="text/javascript">
    function checkOptions(theForm) {
      if (theForm.box1.checked && theForm.box2.checked && !theForm.box3.checked && !theForm.box4.checked) {
        alert('Go to "Page1.htm"');  // window.location.href = "Page1.htm";
      } else {
        alert('Go to "Page2.htm"');  // window.location.href = "Page2.htm";
      }
    }
    </script>
    
    <style type="text/css">
    
    </style>
    </head>
    <body>
     <form name="theForm" onsubmit="return false"> <!-- for testing purposes -->
     <input type="checkbox" name="box1"> Box 1<br>
     <input type="checkbox" name="box2"> Box 2<br>
     <input type="checkbox" name="box3"> Box 3<br>
     <input type="checkbox" name="box4"> Box 4<br>
     <button onclick="checkOptions(this.form)">Go To</button>
     </form>
    </body>
    </html>
    Last edited by JMRKER; 10-22-2012 at 07:34 PM.

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