A javascript script does not work for me

2

I have the following code, the part of css and html5 works very well, but I can not know why the script is not executed, from the button I call the function verificación(); through the event onclick , but when you press the button, absolutely nothing happens.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>algo</title>
<link href="../css/tarea_sesion14.css" rel="stylesheet" type="text/css">

<script>

function verificacion(){
var numero1;
numero1 = document.getElementById("CajaTexto1").value;
if(isNaN(numero1)){
    alert("No escribiste un numero");
}else{
    if(numero1>=1 && numero1<=6){
        for(var i=0: i>numero1: i++){
            document.write("Esta es la vez numero "+i+" que se ejecuta el 
for");
        }
    }else{
        alert("Favor de escribir en la caja de texto un numero entre el 1 y el 6");
    }
 }
}
</script>

</head>
<body>

<div id="contenedor">

<div id="izquierdo">
</div>

<div id="principal">
    <fieldset>
    <form id="formularioTarea14" name="form1" method="post">
        <label >Escribe un numero del 1 al 6:</label>
        <input type="text" id="CajaTexto1">
        <input type="submit" onclick="verificacion();" id="btn_ejecutar" value="Iniciar">
    </form>
    </fieldset>
</div>

</div>

</body>
</html>
    
asked by Nemeium 06.05.2017 в 04:00
source

2 answers

3

This is a Syntax error in for the colon is not accepted to separate the parts of a for

 /* Incorrecto los : */
for(inicializacion: condición :  incremento o decre){...}
/* Correcto el ; */
for(inicializacion; condición ;  incremento o decre){...} 

If you modify your for with this, you should not have syntax errors but of lógica in the condition of for since you will never execute what is inside.

Si ingresa 4 , pregunta  0 > 4  /* retorna false*/
Si ingresa 4 , pregunta  0 > 4  /* retorna false*/

Your final code would be

function verificacion(){
  var numero1 = document.getElementById("CajaTexto1").value;
  if(isNaN(numero1))  alert("No escribiste un numero");
  else{
    if(numero1>=1 && numero1<=6){
        for(var i=0; i<numero1; i++)
            document.getElementById('resultado').innerHTML += "Esta es la vez numero "+(i+1)+" que se ejecuta el for <br>";
    }
    else alert("Favor de escribir en la caja de texto un numero entre el 1 y el 6");
   }
   return false;
}
<input type="text" id="CajaTexto1">
<input type="submit" onclick="verificacion();" id="btn_ejecutar" value="Iniciar">
<div id="resultado">
	
</div>

Be careful about the type of the botón since the form will be sent, to solve this I added the return false so that the delivery is not executed, about document.write that in the documentation there is an important note.

  

Note: since document.write writes directly to the thread (stream) of a   document, the call to document.write in a document already loaded   automatically execute document.open , which will clean all the   content of the document in question.

    
answered by 06.05.2017 / 04:15
source
2

You have several errors. The first one, in the for loop you separate the properties with ' : ' instead of ' ; '
Then, the loop is done while i>numero1 and should be while i<numero1
Another problem, the button is of type submit so it will reload the page, so that only execute the function should be type button .
More things, the method document.write deletes the page if it is used once loaded, as it is the case when using it in button , use another system to show the data, how to increase the innerHTML of some element.
Minor things, so that the message is not written often adds a jump at the end, and if you want it to show a counter starting at 1, you must show it by adding 1, since it really starts with 0. The call to the function does not need to end with ;

Once corrected something similar to this remains:

function verificacion(){
var numero1;
numero1 = document.getElementById("CajaTexto1").value;
if(isNaN(numero1)){
    alert("No escribiste un numero");
}else{
    if(numero1>=1 && numero1<=6){
        for(var i=0; i<numero1; i++){
            document.documentElement.innerHTML+=("Esta es la vez numero "+(i+1)+" que se ejecuta el for<br>");
        }
    }else{
        alert("Favor de escribir en la caja de texto un numero entre el 1 y el 6");
    }
 }
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>algo</title>
<link href="../css/tarea_sesion14.css" rel="stylesheet" type="text/css">

</head>
<body>

<div id="contenedor">

<div id="izquierdo">
</div>

<div id="principal">
    <fieldset>
    <form id="formularioTarea14" name="form1" method="post">
        <label >Escribe un numero del 1 al 6:</label>
        <input type="text" id="CajaTexto1">
        <input type="button" onclick="verificacion()" id="btn_ejecutar" value="Iniciar">
    </form>
    </fieldset>
</div>

</div>

</body>
</html>
    
answered by 06.05.2017 в 04:26