Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Javascript Forms and Buttons?

I cannot for the life of me figure out what is wrong in my code. besides the obvious mathematical incorrectness of the formula. It's not giving me output though. When I hit the button, nothing happens. I might add that where it says res... in the code below, it actually means result semi-colon.

but ya won't let me type it apparently.

<script language="javascript">

function calc()

{ var dist; var fuel; var result; var transtype;

dist=document.carbonfoot.dist.value;

fuel=document.carbonfoot.fuel.value;

result= dist/fuel;

document.carbonfoot.result.value=result;

}

</script>

<form name="carbonfoot">

<input type="radio" name="transtype" value="car">Car

<input type="radio" name="transtype" value="basket">Basket on a rope

<input type="radio" name="transtype" value="horse">Horse

<br/><br/>

Distance Travelled: <input type="text" name="dist"> KM<br/>

Fuel Used: <input type="text" name="fuel"> Litres<br/>

<input type="button" value="calculate" on click="calc()"><br/>

Your Carbon Emissions: <input type="text" name="result">

</form>

3 Answers

Relevance
  • ?
    Lv 7
    10 years ago
    Favorite Answer

    The text in a text box is consider a string even if you put a number in the text box. Before you can do math it's a good idea to convert the string to a number.

    dist = parseFloat(document.carbonfoot.

       dist.value);

    fuel = parseFloat(document.carbonfoot.

       fuel.value);

    Instead of using the <br> tag everywhere, create an unordered list. Then in your stylesheet you can remove the bullet points and adjust the spacing between elements as you see fit.

    <style>

    #myForm ul {

        list-style-type:none;

    }

    #myForm li {

        margin: 1em 0;

    }

    </style>

    Then in your form at the id attribute.

    <form name="carbonfoot" id="myForm">

        <ul>

            <li>

                <input>...

            </li>

            <li>

                <input>...

            </li>

    etc...

    It's also good practice to add labels to your inputs like so.

    <input type="radio" name="transtype" id="car">

    <label for="car">Car</label>

    Now when a user click on the text "Car" it will select the radio button with the id of "car" just as if they clicked on the radio button itself.

  • lakey
    Lv 4
    5 years ago

    can 2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60du p2db5a9c0289167b3831911c5482d60dst 2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60du2db5a9c0289167b3831911c5482d60d c2db5a9c0289167b3831911c5482d60dde s2db5a9c0289167b3831911c5482d60d we are able to help 2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60du an2db5a9c0289167b3831911c5482d60dwa2db5a9c0289167b3831911c5482d60d javac2db5a9c0289167b3831911c5482d60dipt sub2db5a9c0289167b3831911c5482d60dit d2db5a9c0289167b3831911c5482d60dcu2db5a9c0289167b3831911c5482d60dent.2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60ds[2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d].sub2db5a9c0289167b3831911c5482d60dit(); has n2db5a9c0289167b3831911c5482d60dthing t2db5a9c0289167b3831911c5482d60d d2db5a9c0289167b3831911c5482d60d with which butt2db5a9c0289167b3831911c5482d60dn is clicked that's 2db5a9c0289167b3831911c5482d60delated t2db5a9c0289167b3831911c5482d60d the sub2db5a9c0289167b3831911c5482d60ditted 2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d 2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d2db5a9c0289167b3831911c5482d60d in the p2db5a9c0289167b3831911c5482d60devi2db5a9c0289167b3831911c5482d60dus line

  • 10 years ago

    You have the event name wrong in the button markup. It should be onclick instead of on click (remove the space)

Still have questions? Get your answers by asking now.