Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
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 Respuestas
Responde la pregunta

0

Add Following code in your javascript

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

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda