Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
how to add n number of input value with javascript

Friends, I want the sum of the numbers I put in fields and these input value total are equal to my total value. it show a Nan Error When i add these input values please help

This is my body Content

    <form action="" method="post">
    <input type="text" name="cnum" placeholder="enter no. of input">
    <input type="submit" name="submit" >
    </form>
    <?php
    if(isset($_POST['cnum']))
    {
        $cnum=$_POST['cnum'];
        $i=0;
        ?>
    <form action="" method="post">
        <input type="hidden" name="total" id="total" value="10">
        <input type="hidden" name="cnum" id="cnum" value="<?php echo $cnum ;?>">
        <?php
        while($i<$cnum)
        {
            ?>
                <input type="number" id="aq<?php echo $i; ?>" onKeyup="add()" name="aq<?php echo $i; ?>"><br />
            <?php
            $i++;
        }
    ?>
        <input type="submit" name="aqsub">
    </form>
    <?php
    }
    ?>

This is my java script function

var val=0;
var total=document.getElementById('total').value;
var cnum=document.getElementById('cnum').value;
var anum;
function add()
{
var n=0;
while(n<cnum)
{
    anum = 0;
    if(document.getElementById('aq'+n).value != "")        {            
            anum=parseInt(document.getElementById('aq'+n).value);
        }
    val += anum;
    n++;
}
alert(val);

}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Add Following code in your javascript

cnum = parseInt(cnum);
over 4 years ago · Santiago Trujillo Report

0

Update code as:

var total= parseInt(document.getElementById('total').value);

As you are using total to comparing with anum. so must be error Nan for total

over 4 years ago · Santiago Trujillo Report

0

Well I don't understand your logic but you get NAN because your value of fields are blank.

try this code

var val=0;
var total=document.getElementById('total').value;
var cnum=document.getElementById('cnum').value;
var anum;
function add()
{
    var n=0;
    while(n<cnum)
    {
        anum = 0;
        if(document.getElementById('aq'+n).value != "")        {            
                anum=parseInt(document.getElementById('aq'+n).value);
            }
        val += anum;
        n++;
    }
    if(total==anum)
    {
        alert('Its Work');
    }
    else
    {
        alert('Its not Work');
    }

}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!