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

  • SEARCH
  • Home
  • 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 8631545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:14:03+00:00 2026-06-12T09:14:03+00:00

Aim: This chunk of js, is designed to grab values entered into fields in

  • 0

Aim: This chunk of js, is designed to grab values entered into fields in a form, assign them to variables and then perform a calculation which outputs into a field on that page.

There is a short function that means out put is set as Unknown if any of the variables are undefined or empty.

My code:

function calculate() {
    var $u5pop = $('input[name=sv_21d]').val();
    var $sam = $('input[name=sv_25d]').val();
    var $incidence = $('input[name=sv_313]').val();
    var $treated = $('input[name=sv_312]').val();


    if ($u5pop == undefined ||$u5pop == '') {
        $u5pop = 0;
    }
    else {
        $u5pop = parseFloat($u5pop);
    }

    if ($sam == undefined ||$sam == '') {
        $sam = 0;
    }
    else {
        $sam = parseFloat($sam);
    }

    if ($incidence == undefined || $incidence == '') {
        $incidence = 0;
    }
    else {
        $incidence = parseFloat($incidence);
    }
    //
    if ($treated == undefined || $treated == '') {
        $treated = 0;
    }
    else {
        $treated = parseFloat($treated);
    }

    var $determined = $u5pop / $incidence * $treated * $sam;

    if ($determined == undefined || $determined == '') {
        $determinedOut = 'Unknown';
    }
    else {
        $determinedOut = parseFloat($determined);
    }

    $('#calculation').attr('value', $determinedOut.toFixed(2));
}

The output field div #calculation is:

<input size="18" class="calculation" id="calculation" value="Click to calculate" onfocus="calculate()"/>

Such that when user click within the field it performs the calculation.

This is a structure / syntax issue I think, but everything looks good to me. Would anyone with more experienced eyes be able to see where I’m going wrong here?
At current: nothing happens (the default value remains).

Many thanks!

EDIT: This works perfectly so long as all the fields are filled in, however it FAILSS to output ‘unknown’ if the fields are left empty and the function is called by onfocus

  • 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-06-12T09:14:03+00:00Added an answer on June 12, 2026 at 9:14 am

    See this jsFiddle. The code failed to change the input value initially until I moved the JS function above the HTML. Perhaps that’s your issue!?

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

Sidebar

Related Questions

how do you covert this html code into the yii CHtml? <form aciton='site/qrcode' method='POST'>
The aim behind asking this question is to improve my understanding of Java Proxies
My aim is to create a map function like this map = function ()
My ultimate aim is to have a table that looks like this: Maturity Band
This is a general question concerning technology decisions for a product development. My aim
could somebody please take a look at this http://jsfiddle.net/bloodygeese/EzkFR/1/ My aim is to on
My focus in this question is making HTML5 games. The aim is to help
I'm saving contacts (email, mobile phone, ICQ, AIM etc.) for people like this: class
Aim : To convert a integer value first to hexstring and then to byte[].
I am using this site. Aim : I want to move my footer based

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.