Help with function


#1

Hello,

Can someone help me to know what its wrong with this function? Its not running and i cant figure out what its wrong, thanks:

<html>
<body>
<form>

 <fieldset>
 <div>
  <legend>Ejercicio 5</legend>
  <label for="Valor1">Valor1</Label>
  <input type="number" id="Valor1_id" name="Valor1" placeholder="Ingresar Valor1"><br>
  <label for="Valor2">Valor2</label>
  <input type="number" id="Valor2_id" name="Valor2" placeholder="Ingresar Valor2"><br>
  <input type="button" id="btnCalcular" value="Calcular"><br>
   </fieldset>
       </form>
	      </div>
     <br>
	 <hr>
	 <div id="resultado1"></div>
	 <div id="resultado2"></div>
	
	<script>
	function btnCalcular(){
	 var Valor1=document.getElementById("Valor1_id").value;
	 var Valor2=document.getElementById("Valor2_id").value;
	 document.getElementById("resultado1").innerHTML=(Valor1+Valor2);
	 var Valor1=document.getElementById("Valor1_id").value;
	 var Valor2=document.getElementById("Valor2_id").value;
	 document.getElementById("resultado2").innerHTML=(Valor1*Valor2);
	 }
	
	
	</script>



   </body>
   </html>

#2

You have some elements chained. Move the <div> to before the <form> and that will cure it.

As for your script, you do not need to repeat the getElementById.

<script>
// define and cache node objects in advance
var valor1 = document.getElementById("Valor1_id");
var valor2 = document.getElementById("Valor2_id");
var resultado1 = document.getElementById("resultado1");
var resultado2 = document.getElementById("resultado2");

function btnCalcular() {
    resultado1.innerHTML = valor1.value + valor2.value;
    resultado2.innerHTML = valor1.value * valor2.value;
}
</script>

Edited => value() -> value


#3

thank you, for other side, i cant see the error on the following function, the n°2 excercise, can you please help me:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="js/jquery-3.3.1.min.js" type="text/javascript"></script>
        <script src="js/codigo.js" type="text/javascript"></script>
    </head>
    <body>
        <div>
        <h1>Ejercicio 1</h1>
        <label for="txtNombre">Nombre</label>
        <input type="text" id="txtNombre"><br>
        <label for="txtApellido">Apellido</label>
        <input type="text" id="txtApellido"><br>
        <input type="button" id="btnprocesarEj1" value="Procesar">
        <hr>
        </div>
        
        <p id="pResultado"></p>
        <hr>
        
        <div>
            
            <h1>Ejercicio2</h1>
            
            <label for="txtvalor1">valor1</label>
            <input type="text" id="txtvalor1"><br>
            <label for="txtvalor2">valor2</label>
            <input type="text" id="txtvalor2"><br>
            <input type="button" id="btnsumaEj2" value="suma">
            
            <p id="pMostrar"></p>
            
            <hr>
            
           
           
        </div>
        
        
    </body>
</html>

$().ready(iniciar);

function iniciar() {
$("#btnprocesarEj1").click(procesarEjercicio1);
$("#btnsumaEj2").click(procesarEjercicio2);
}

function procesarEjercicio1() {
var txtNombre=$("#txtNombre").val();
var txtApellido=$("#txtApellido").val();
var txtResultado=txtApellido +","+ txtNombre;

$("#pResultado").html(txtResultado);
}




function procesarEjercicio2(){
    var txtvalor1=parseInt($("#txtvalor1").val());
    var txtvalor2=parseInt($("#txtvalor2").val());
    var txtMostrar=txtvalor1+txtvalor2;
   
   $("pMostrar").html(txtMostrar);
}
type or paste code here

Thanks


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.