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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Using a button to call a function

    I am creating a simple program that converts degrees Celsius to Fahrenheit, and vice versa. I have a set of radio buttons for choosing a conversion direction, then a textbox for inputting the degrees. I created a button below it all to "Calculate" the degree conversion, and I want the derived value to be displayed under the button after it is pressed. Here is my code:

    Code:
    <p id="directionLabel">Please select a direction of conversion:</p>
    <p><input type="radio" id="dir1" name="direction" value="0" checked="true"/>Celsius to Fahrenheit</p>
    <p><input type="radio" id="dir2" name="direction" value="1"/>Fahrenheit to Celsuis</p>
    <p id="degreeLabel">Please enter numbers only for degrees.</p>
    <p><input type="text" id="degreetext" name="degrees" value="Degrees"/></p>
    <p><input type="button" name="calculate" value="Calculate" onClick="Calculate()"/></p></center></td></tr></table>
    <script type="text/javascript>
    var initial=document.getElementById("degreetext").value;
    var inval=parseInt(initial);
    function Calculate(){
    if (document.getElementById("dir1").checked="true"){document.writeln(initial + "degrees Celsius is equal to" +
    	(9/5)*inval+32 + "degrees Fahrenheit.")};
    else{
    document.writeln(initial+"degrees Fahrenheit is equal to"+
    	(5/9)*(inval-32)+"degrees Celsius.")}
    	
    </script>
    Right now, all of the structural elements are visible, but when I fill in the fields and click the Calculate button, nothing (seems) to happen. I'm assuming it is a problem with the function itself, not the function's association with the button, but I'm not sure.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,619
    Thanks
    0
    Thanked 645 Times in 635 Posts
    1. You are retrieving the value from the degree text field before it gets changed from "Degrees"

    2. The parseInt will return 0 as there is no number in that field.

    3. The document.writeln statements will overwrite the entire page with a new one that consists soley of the text "0degrees Celsius is equal to 32 degrees Fahrenheit."

    4. Your if statement is the equivalent of:

    document.getElementById("dir1").checked="true";
    if (document.getElementById("dir1").checked)


    which is always true because the any string except "" or "0" evaluates as true when converted from a string to a boolean.
    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
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    PLUS
    You don't end the Calculate() function with the correct number of brackets.

    AND
    You have a table reference without table definitions.

    Here's a revision using as much of your code a possible.
    Note: Changed the "Calculate()" function to "CalculateTemps()" to be a bit more descriptive.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Temperature Converter </title>
    
    </head>
    <body>
    <p>Please select a direction of conversion:</p>
    <p><input type="radio" name="direction" id="dir1" checked />Celsius to Fahrenheit</p>
    <p><input type="radio" name="direction" />Fahrenheit to Celsuis</p>
    <p id="degreeLabel">Please enter numbers only for degrees.</p>
    <p><input type="text" id="degreetext" value="0" /> Degrees</p>
    <p><input type="button" value="Calculate" onClick="CalculateTemps()" /></p>
    <p><div id="results"></div></p>
    
    <script type="text/javascript">
    
    function CalculateTemps() {
      var initial=document.getElementById("degreetext").value;
      var inval=parseInt(initial);
      var degrees = '';
      if (document.getElementById("dir1").checked) 
           { degrees = " degrees Celsius is equal to " + ((9/5)*inval+32).toFixed(1) + " degrees Fahrenheit."; }
      else { degrees = " degrees Fahrenheit is equal to "+ ((5/9)*(inval-32)).toFixed(1)+" degrees Celsius."; }
      document.getElementById('results').innerHTML = initial + degrees;
    }
    	
    </script>
    </body>
    </html>

  • Users who have thanked jmrker for this post:

    skywaves (02-11-2014)


  •  

    Tags for this Thread

    Posting Permissions

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