Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4606802
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:36:56+00:00 2026-05-22T00:36:56+00:00

Doubtful this needs much explaining but I think I need a second set of

  • 0

Doubtful this needs much explaining but I think I need a second set of eyes to check this out because I just cannot fathom why it won’t! Try using the “Energy Calculator” and no value will be returned when submitted despite me approaching it in the same way as the “Battery Charge Calculator” which does work.

Link:
http://jsfiddle.net/Deva/426Jj/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:36:57+00:00Added an answer on May 22, 2026 at 12:36 am

    To elaborate on @alex’s answer, you have selected the onLoad option of jsFiddle, which places your JavaScript code inside of an anonymous function:

    window.onload = function() {
        function energyCalc() {
            var mass = document.forms['0'].mass.value;
            var finalMass = mass * 1000;
            var velocity = document.forms['0'].velocity.value;
            var finalVelocity = velocity * 0.3048;
            var energy = (0.5 * (finalMass * (finalVelocity * finalVelocity)));
            if ((mass && velocity) != '') {
                document.getElementById("energy").innerHTML = 'Energy of weapon: ' + energy + ' Joules';
            } else {
                document.getElementById("energy").innerHTML = 'You must enter values in both fields!';
            }
        }
    }
    

    Try this on for size: http://jsfiddle.net/mattball/Cbsa8/. All I did was select no wrap (head) and No library.

    Keep in mind that it’s better coding practice to write unobtrusive JavaScript. In this case you’d bind the event handler with JavaScript, using attachEvent (for IE <9) or addEventListener (for real browsers).


    Edit re: OP edits

    Open a console and the problem is immediately obvious after trying to use the Energy Calculator:

    > Uncaught TypeError: Cannot read property 'value' of undefined (fiddle.jshell.net:100)
    

    which is this line:

    var mass = document.forms['0'].mass.value;
    

    You’re accessing the wrong form. You need to change all cases of forms['0'] to forms[1] in the energy calculator’s code. Note the omitted quotes, by the way – document.forms is an array, not an object!

    Also, you’re clearly using jQuery, but you’re not using it everywhere you could. Why not? There’s a lot of room for improvement in your code. It’s cleanup time!

    HTML

    <div id="main">
        <a href="#"><h3>Battery Charge Calculator</h3></a>
        <div class="slide">
            <form id="batteryForm" action="">
                <p>
                    Battery Capacity (mah):
                </p>
                <p>
                    <input name="capacity"/>
                </p>
                <p>
                    Charge Rate (mA):
                </p>
                <p>
                    <input name="rate"/>
                </p>
                <p>
                    <input type="submit" value="Submit"/>
                </p>
                <br/>
                <p id="time"/>
            </form>
        </div>
        <a href="#"><h3>Energy Calculator</h3></a>
        <div class="slide">
            <form id="energyForm" action="">
                <p>
                    Mass of BB (grammes):
                </p>
                <p>
                    <input name="mass"/>
                </p>
                <p>
                    Power of weapon (FPS):
                </p>
                <p>
                    <input name="velocity"/>
                </p>
                <p>
                    <input type="submit" value="Submit"/>
                </p>
                <br/>
                <p id="energy"/>
            </form>
        </div>
    </div>
    

    JavaScript

    $(function() {
        $('#main > a > h3').click(function() {
            $(this).closest('a').next('div.slide').animate({
                height: 'toggle'
            }, 750);
        });
    
        function batteryCalc() {
            var capacity = this.capacity.value,
                rate = this.rate.value,
                time = capacity / (rate / 1.2),
                chargeTime = (Math.round(10 * time) / 10).toFixed(1),
                message = 'You must enter values in both fields!';
    
            if (capacity && rate) {
                message = 'Required time on charge: ' + chargeTime + ' hours';
            }
    
            $('#time').html(message);
            return false;
        }
    
        function energyCalc() {
            var mass = this.mass.value,
                finalMass = mass * 1000,
                velocity = this.velocity.value,
                finalVelocity = velocity * 0.3048,
                energy = (0.5 * (finalMass * (finalVelocity * finalVelocity))).toFixed(1),
                message = 'You must enter values in both fields!';
    
            if (mass && velocity) {
                message = 'Energy of weapon: ' + energy + ' Joules';
            }
    
            $('#energy').html(message);
            return false;
        }
    
    
        $('#batteryForm').submit(batteryCalc);
        $('#energyForm').submit(energyCalc);
    });
    

    Demo: http://jsfiddle.net/mattball/JSpaU/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I intend to develop an SMS gateway in c#, but i am doubtful about
Not sure if I am missing something very blatant or (much more doubtful) I
I need to store a customer 's currently selected payment info ,so that this
Although I'm doubtful, I'm curious as to whether it's possible to extract primitive-type template
i am doubtful about it that vc++6.0 is .Net framework independent or depends on
So I need to connect to a mssql server via Windows Authentication from a
I'm reviewing a manual of best practices and recommendation coding java I think is
I'm sure this ask is incomplete without a precise context, so I'll try to
This is a really weird problem. It's taken me practically all day to whittle
As I was reading this ( Find the most common entry in an array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.