MixedUpCody's Profile User Rating: -----

Reputation: 1 Apprentice
Group:
Members
Active Posts:
49 (0.06 per day)
Joined:
24-December 11
Profile Views:
652
Last Active:
User is offline Oct 13 2013 07:51 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   MixedUpCody Awesome

Posts I've Made

  1. In Topic: Dynamic Form Text Update

    Posted 13 Oct 2013

    andrewsw,

    Yes, I was getting a "firstChild is NULL" error on Firebug, so I changed the code and now it works. Here is the final code. Thanks for your help, everybody.

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Calculate MPG</title>
       	<link rel="stylesheet" href="mpg.css">
       	<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>	
       	<script>
       		var $ = function (id) {
    		    return document.getElementById(id);
    		};
        	var calculateMpg = function () {
    			var miles = parseFloat($("miles").value);
    			var gallons = parseFloat($("gallons").value);
    			
    			if (isNaN(miles)) {
    				alert("Miles must be numeric");
    			} else if (miles <= 0){
    				alert("Miles must be greater than 0.");
    			} else if (isNaN(gallons)){
    				alert("Gallons must be numeric");
    			} else if (gallons <= 0){
    				alert("Gallons must be greater than 0.");
    			}
    			else {
    				var mpg = miles / gallons;
    				$("mpg").value = mpg.toFixed(1);
    			}
    		};
    		var clear = function () {
    			$("miles").value = "";
    			$("gallons").value = "";
    		};
    		window.onload = function () {
    		    $("calculate").onclick = calculateMpg;
    		    $("clear").onclick = clear;
    		    $("miles").ondblclick = clear;
    		    $("miles").focus();
    		};
    		    
       	</script>
    </head>
    <body>
        <section>
            <h1>Calculate Miles Per Gallon</h1>
            <label for="miles">Miles Driven:</label>
    	    <input type="text" id="miles"><br>
            <label for="gallons">Gallons of Gas Used:</label>
            <input type="text" id="gallons"><br>
            <label for="mpg">Miles Per Gallon</label>
            <input type="text" id="mpg" disabled><br>
            <label>&nbsp;</label>
            <input type="button" id="calculate" value="Calculate MPG"><br>
            <label>&nbsp;</label>
            <input type="button" id="clear" value="Clear Entries"><br>
        </section>
    </body>
    </html>
    
  2. In Topic: Dynamic Form Text Update

    Posted 13 Oct 2013

    BetaWar,

    Yea, I am learning JS, and the book exercise says to do it this way. Any ideas?

    Cody
  3. In Topic: Murach's JS Book Question

    Posted 5 Oct 2013

    Thanks again. I'll indent correctly next time.

    Cody
  4. In Topic: Murach's JS Book Question

    Posted 5 Oct 2013

    The spelling fixed it. Thank you. I see the indentation error. Thank you for pointing that out, as well.

    Cody

    EDIT: Two posts showed up as one.
  5. In Topic: Undefined Variable in div

    Posted 8 Sep 2013

    Okay. I'll get that straight next time. Thank you.

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Friends

Comments

MixedUpCody has no profile comments yet. Why not say hello?