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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    help with basic javascript for uni assignement please! :-)

    hi all, just got to make a basic form for my assignemment but i cant seem to get it to work. please could somebody point me in the right direction? thank you

    Code:
    <head>
    
    <script type="text/javascript">
    
    function make(myform)
    {
    var name = form.nameinput.value ;
    var age = form.ageinput.value ;
    var location = form.locationinput.value ;
    var z = form.team.selectedIndex ;
    var team = form.team.options[z].value;
    alert("Thank you " + name + ".Good luck supporting " + team + );
    }
    </script>
    
    </head>
    
    <body>
    
    <form method="get" action="" name="myform">
    <h2>Order form</h2>
    <fieldset>
    <p>Please enter your details</p>
    Name: <input type="text" value="" name="nameinput"/><br/>
    Age: <input type="text" value="" size="2" name="ageinput"/><br/>
    Location: <input type="text" value="" name="locationinput"/>
    
    <p>Choose your favourite football team</p>
    <select name="team">
    <option value="arsenal">Arsenal</option>
    <option value="aston_villa">Aston Villa</option>
    <option value="birmingham_city">Birmingham City</option>
    <option value="blackburn_rovers">Blackburn Rovers</option>
    <option value="bolton_wanderers">Bolton Wanderers</option>
    <option value="burnley">Burnley</option>
    <option value="chelsea">Chelsea</option>
    <option value="everton">Everton</option>
    <option value="fulham">Fulham</option>
    <option value="hull">Hull</option>
    <option value="liverpool">Liverpool</option>
    <option value="manchester_city">Manchester City</option>
    <option value="manchester_united">Manchester United</option>
    <option value="portsmouth">Portsmouth</option>
    <option value="stoke_city">Stoke City</option>
    <option value="sunderland">Sunderland</option>
    <option value="tottenham_hotspur">Tottenham Hotspur</option>
    <option value="west_ham">West Ham</option>
    <option value="wigan_athletic">Wigan Athletic</option>
    <option value="wolverhampton_wanderers">Wolverhampton Wanderers
    </option>
    </select>
    </fieldset>
    <input type="button" onclick="make(this.form)" value="Submit Details" name="button"/>
    </form>
    Last edited by sjjs1985; 12-06-2009 at 04:58 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Check this out:-

    Code:
    <script type="text/javascript">
    
    function make(form) {
    var name = form.nameinput.value ;
    var age = form.ageinput.value ;
    var location = form.locationinput.value ;
    var z = form.team.selectedIndex ;
    var team = form.team.options[z].value;
    alert("Thank you " + name + ". Good luck supporting " + team + );
    }
    </script>
    Bookshop Assistant: "This book about Javascript will save you half your work".
    Lazy Student: "Oh good! I'll take two!"

  • #3
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks for the reply but still no luck

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Well, it works fine for me. Check your error console. Did you correct the alert syntax?

    BTW, you would do better to change
    var team = form.team.options[z].value;
    to
    var team = form.team.options[z].text;

    so that they are thanked for supporting
    Blackburn Rovers
    rather than
    blackburn_rovers

    or make the option values the same as the text.

    Note that
    function make(form) { //opening brace on same line
    is to be preferred to
    function make(form)
    {
    to avoid the risk of a fatal error of putting a semi-colon after function make(form)


    BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in [code] tags. This means use the octothorpe or # button on the toolbar which will insert opening [ code ] and closing [ /code ] tags - omit the spaces. You can (and should) edit your previous post.
    Last edited by Philip M; 12-06-2009 at 04:52 PM.

  • Users who have thanked Philip M for this post:

    sjjs1985 (12-06-2009)

  • #5
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Quote Originally Posted by sjjs1985 View Post
    hi all, just got to make a basic form for my assignemment but i cant seem to get it to work. please could somebody point me in the right direction?
    FireFox -> Tools / Error Console

  • Users who have thanked randomuser773 for this post:

    sjjs1985 (12-06-2009)

  • #6
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    my error console says:

    Error: syntax error
    Source code:
    alert("Thank you " + name + ". Good luck supporting " + team + );


    and an arrow points to the last bracket.

    all help greatly appreciated. thanking you for your time. sam

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    I have already pointed out to you the error:-

    alert("Thank you " + name + ". Good luck supporting " + team + );

  • #8
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    ah stupid me. i didnt realise your red font meant it shouldnt be there! thanks all for the help!!


  •  

    Posting Permissions

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