Jump to content

Help with HTML Javascript?

Beeeyeee

this is for an assignment. I have to use a text box to get a value in Miles. Then when the button is pressed it will show the conversion to Kilometers as well. (1 mile equals ~0.6km)

it all works but the value for miles entered be the user shows up as "undefined" and the conversions to kilometers shows up as Not a number... What am I doing wrong?

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <script type="text/javascript">

        function Convertion(){
        var Distance_Miles;
        Distance_Miles= document.getElementById("Distance_Miles").value;
        Distance_Kilometers= Distance_Miles * 0.6;
        document.getElementById("Distance_Miles").innerHTML= "Your distance is " + Distance_Miles + " Miles. That's " + Distance_Kilometers + " Kilometers!";
        }
    </script>
    </head>

    <body>
            <h1>HTML!</h1>
<div>
    <h2 id=Distance_Miles>Convert Miles to Kilometers</h2>
    <p><label for="Distance_Miles"> Give me a number</label>
    <input type="text" id="Distance_Miles"/></p>
    <p><button onclick="Convertion();">Get Distance</button></p>
</div>

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Beeeyeee said:

this is for an assignment. I have to use a text box to get a value in Miles. Then when the button is pressed it will show the conversion to Kilometers as well. (1 mile equals ~0.6km)

it all works but the value for miles entered be the user shows up as "undefined" and the conversions to kilometers shows up as Not a number... What am I doing wrong?

 

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <script type="text/javascript">

        function Convertion(){
        var Distance_Miles;
        Distance_Miles= document.getElementById("Distance_Miles").value;
        Distance_Kilometers= Distance_Miles * 0.6;
        document.getElementById("Distance_Miles").innerHTML= "Your distance is " + Distance_Miles + " Miles. That's " + Distance_Kilometers + " Kilometers!";
        }
    </script>
    </head>

    <body>
            <h1>HTML!</h1>
<div>
    <h2 id=Distance_Miles>Convert Miles to Kilometers</h2>
    <p><label for="Distance_Miles"> Give me a number</label>
    <input type="text" id="Distance_Miles"/></p>
    <p><button onclick="Convertion();">Get Distance</button></p>
</div>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <script type="text/javascript">

        function Conversion(){
        var Distance_Miles;
        var Distance_Kilometers ;
        Distance_Miles= document.getElementById("Distance_Miles").value;
        Distance_Kilometers= Distance_Miles * 0.6;
        document.getElementById("conversionText").innerHTML= "Your distance is " + Distance_Miles + " Miles. That's " + Distance_Kilometers + " Kilometers!";
        }
    </script>
    </head>

    <body>
            <h1>HTML!</h1>
<div>
    <h2 id="conversionText">Convert Miles to Kilometers</h2>
    <p><label for="Distance_Miles"> Give me a number</label>
    <input type="text" id="Distance_Miles"/></p>
    <p><button onclick="Conversion();">Get Distance</button></p>
</div>

changed a few div id's around and it seems to work.

An ID is supposed to be unique for each element.

Also changed the function name to conversion and not convertion

Distance_Kilometers was also not being defined.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, leonfagan71 said:

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <script type="text/javascript">

        function Conversion(){
        var Distance_Miles;
        var Distance_Kilometers ;
        Distance_Miles= document.getElementById("Distance_Miles").value;
        Distance_Kilometers= Distance_Miles * 0.6;
        document.getElementById("conversionText").innerHTML= "Your distance is " + Distance_Miles + " Miles. That's " + Distance_Kilometers + " Kilometers!";
        }
    </script>
    </head>

    <body>
            <h1>HTML!</h1>
<div>
    <h2 id="conversionText">Convert Miles to Kilometers</h2>
    <p><label for="Distance_Miles"> Give me a number</label>
    <input type="text" id="Distance_Miles"/></p>
    <p><button onclick="Conversion();">Get Distance</button></p>
</div>

changed a few div id's around and it seems to work.

An ID is supposed to be unique for each element.

Also changed the function name to conversion and not convertion

oh awesome thanks man. sometimes its the little things I don't realize. you're the best.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Beeeyeee said:

oh awesome thanks man. sometimes its the little things I don't realize. you're the best.

Okay, no worries. PM me if you have any issues and I'll see what I can do to help in the future.

 

Cheers,

Leon.

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, Beeeyeee said:

oh awesome thanks man. sometimes its the little things I don't realize. you're the best.

Although it works, one should be using lowerCamelCase when creating variables and functions.

See Google's Javascript Styling Guide for more information.

Desktop: KiRaShi-Intel-2022 (i5-12600K, RTX2060) Mobile: OnePlus 5T | REDACTED - 50GB US + CAN Data for $34/month
Laptop: Dell XPS 15 9560 (the real 15" MacBook Pro that Apple didn't make) Tablet: iPad Mini 5 | Lenovo IdeaPad Duet 10.1
Camera: Canon M6 Mark II | Canon Rebel T1i (500D) | Canon SX280 | Panasonic TS20D Music: Spotify Premium (CIRCA '08)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×