Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-30-2012, 01:52 AM   PM User | #1
masterchief07
New to the CF scene

 
Join Date: Apr 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
masterchief07 is an unknown quantity at this point
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.
masterchief07 is offline   Reply With Quote
Old 04-30-2012, 03:02 AM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,462
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 04-30-2012, 05:24 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 04-30-2012, 06:54 AM   PM User | #4
masterchief07
New to the CF scene

 
Join Date: Apr 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
masterchief07 is an unknown quantity at this point
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.
masterchief07 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:35 AM.


Advertisement
Log in to turn off these ads.