Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Basic Code Help

  1. #1
    New to the CF scene
    Join Date
    Mar 2014
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Basic Code Help

    Im new to the forum and new to JavaScript. Im doing a project and am trying to display a lineup in the text box when that team is picked from the option menu. The code I have entered does not produce the desired result. Any help pointing me in the right direction would be appreciated. Thanks.

    Here is what I have:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <title>Show Lineup</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    /* <![CDATA[ */
    function showLineup() {
    var lineupText = document.form["mlblineups"].lineup;
    var kcLineup = 1b Eric Hosmer,
    2b Omar Infante, Lf Alex Gordon, Dh Billy Butler, C Salvador Perez, 3b Mike Moustakis, Cf Jarrod Dyson, Rf Lorenzo Cain, SS Alcides Escobar.;
    if (document.form["mlbLineups"].teamNames.value == "kcRoyals"); {
    lineupText == kcLineup;
    }
    }

    /* ]]> */
    </script>
    </head>

    <body>
    <form name="mlbLineups">



    <id="teams">
    <label>Which lineup do you want to see?:</Label>
    <select name="teamNames">
    <option value="blank">------------</option>
    <option value="kcRoyals">Royals</option>
    <option value="mnTwins">Twins</option>
    <option value="chicagoWhitesox">White Sox</option>
    <option value="detroitTigers">Tigers</option>
    <option value="clevlandIndians">Indians</option>

    </select>
    <textarea name="lineup" rows="5" cols="35">Lineup:</textarea>
    <br/>


    </form>

    </body>

    </html>

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts
    The kcLineup variable looks like it is supposed to be a string value. Strings must be delimited by quotation marks (either single or double, as long as they match). Also, strings may not contain hard linebreaks unless preceeded with a single backslash (\).
    Code:
    var kcLineup = "1b Eric Hosmer, 2b Omar Infante, Lf Alex Gordon, Dh Billy Butler, C Salvador Perez, 3b Mike Moustakis, Cf Jarrod Dyson, Rf Lorenzo Cain, SS Alcides Escobar.";

  • Users who have thanked kansel for this post:

    sachmo33 (03-02-2014)

  • #3
    New to the CF scene
    Join Date
    Mar 2014
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you for your reply. Even after doing that I can't get the results to show up in the text box. I feel as though I am using the document.form method incorrectly, but can't figure out where.

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts
    There are a number of errors in your original besides the missing quote marks. Here I've fixed all of them and added a missing piece. I also commented above each error.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    <title>Show Lineup</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    /* <![CDATA[ */
    function showLineup() {
    // 1. The correct property name is forms, with an s.
    // 2. Javascript is case sensitive so mlblineups is not the same as mlbLineups.
      var lineupText = document.forms["mlbLineups"].lineup;
      var kcLineup = "1b Eric Hosmer, 2b Omar Infante, Lf Alex Gordon, Dh Billy Butler, C Salvador Perez, 3b Mike Moustakis, Cf Jarrod Dyson, Rf Lorenzo Cain, SS Alcides Escobar.";
    // 3. There was originally a semicolon (;) between the closing parenthesis and opening curly brace at the end of the line below - this basically bypassed the if statement so I removed it.
      if (document.forms["mlbLineups"].teamNames.value == "kcRoyals"){
    // 4. You need to put the value of kcLineup into the value property of lineupText.
    // 5. One equals sign means assignment, two or three means compare for equality.  you want assignment here
        lineupText.value = kcLineup;
      }
    }
    
    /* ]]> */
    </script>
    </head>
    
    <body>
    <form name="mlbLineups">
    
    
    <!-- 6. I don't know what the following tag was supposed to be but it is invalid -->
    <id="teams">
    <label>Which lineup do you want to see?:</Label>
    <!-- 7. You were missing a way for the form to activate the script.  That's what I included here -->
    <select name="teamNames" onchange="showLineup()">
    <option value="blank">------------</option>
    <option value="kcRoyals">Royals</option>
    <option value="mnTwins">Twins</option>
    <option value="chicagoWhitesox">White Sox</option>
    <option value="detroitTigers">Tigers</option>
    <option value="clevlandIndians">Indians</option>
    
    </select>
    <textarea name="lineup" rows="5" cols="35">Lineup:</textarea>
    <br/>
    
    
    </form>
    
    </body>
    
    </html>
    All that said, there are several ways you could optimize this interaction.
    Last edited by kansel; 03-02-2014 at 10:57 PM. Reason: added one more fix

  • Users who have thanked kansel for this post:

    sachmo33 (03-03-2014)

  • #5
    New to the CF scene
    Join Date
    Mar 2014
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you for your help, it worked great. I know there are better ways to do it, but I am just starting and was stuck. I appreciate it.


  •  

    Posting Permissions

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