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

    Simple JS Code - Just need some minor assistance

    Well, the title speaks for itself. What I am trying to do is the following:

    Code:
    Create Form with the following elements:
    
    One Text box, Temp, to accept a temperature value. Two command buttons
    
    -One to invoke a function described in the next step
    -One to RESET form elements
    
    Than:
    
    Does your Last Name begin with A- L ?
    
    -If answered yes: is temp >80?
                    -if answered yes: display “it is okay out there” in back letters
    			Than (end)
                    -if answered no: display: “ it is sweltering out there” in red letters
                            Than (end)
    
    -If answered no: is temp <60?
                    -if answered yes: display: “its is freezing out there” in blue letters
                            Than (end)
                    -If answered no: display: “ it is okay out there” in black letters
                            Than (end)
    And here is the code I have thus far:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var temp = 90;
    var first = true;
    
    if(first == true){
    	if(temp >= 80){
    		document.write("It is okay out there.");
    	}else if(temp <= 60){
    		document.write("It is freezing out there.");
    	} else {
    		document.write("It is okay out there.");
    	}}
    else if(temp >= 80){
    		document.write("It is okay out there.");
    	}else if(temp <= 60){
    		document.write("It is freezing out there.");
    	} else {
    		document.write("It is okay out there.");
    	}
    </script>
    </head>
    <body>
    <FORM NAME="nametemp">
    <H1>Last Name & Weather Questionaire</H1>
    <p><strong>Does your Last Name begin with A- L?</strong></p>
    <INPUT TYPE="BUTTON" VALUE="Yes" onClick="first.value='true'"><INPUT TYPE="BUTTON" VALUE="No" onClick="first.value='false' ">
    <p>
      <INPUT TYPE="TEXT" NAME="tempf" VALUE="0" SIZE=4 MAXLENGTH=6> 
      <font size="+1">Temperature</font>
      <INPUT TYPE="BUTTON" VALUE="Set Temp Variable" onClick="temp.value='' "></br></br>
      </p>
      <input type="reset" value="Reset!">
      <br>
    </FORM></body>
    </html>
    Obviously, it does not work. I have never written JS before and am trying to help out my sister who is taking a CS course. Either way, what I think I am doing wrong has to do with the form submissions. The ONCLICK parameters don't seem to be setting the VAR's to the proper values, or even at all. I've seen some scripts where the ONCLICK is in the JS itself and the HTML just contains references to the ID's established in the script, but shouldn't this work too? Hope you can help and thanks in advance!

    Regards.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 Posts
    You can't use document.write after the page loads.

    Set up a div with an id and use innerHTML instead.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    More than that, your buttons are only changing the state of first and that's not at all what the assignment says. Read the assignment again.

    I don't see any place in the assignment where it tells you to ask the person what their first name starts with. I think when it says YOUR last name, it means exactly that: YOUR name. If not, then you didn't show us the complete assignment. Your form has FOUR buttons, not just the two called for.

    Maybe your sister should do her own work?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I should have clarified. The variable 'first' simply stands for the first variable I assigned. It has no correlation to first name.

    And I'll give it a shot felgall. Thanks.


  •  

    Posting Permissions

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