1

What's wrong? I have created the code and it works, but for some special reason I don't know, it reloads when it stops generating the "table" and I lose the generated code.

Here's the markup

<body>
    <form action="" name="calculadora">
        <p>
            ¿De qué tamaño quieres el determinante? (<span style="font-weight:bold;font-size: 20px">n</span>x<span style="font-weight:bold; font-size:20px">n</span>)
            <input type="text" name="number" size="2"> <button onclick="generate()">Generar cuadrícula</button>
        </p>
        <div id="determinante">

        </div>
        <button onclick="">Caclular</button>
        <div id="resultado">
            <output name="result" for="determinante" style="background='red';"></output>
        </div>
    </form>
    <script src="calculate.js"></script>
</body>

Then the JS code:

function generate(){

var number = document.calculadora.number.value,
    determinante = document.getElementById("determinante");


function generarDeterminante(number) {
    for (var i = 0; i < number; i++) {
        for (var j = 0; j < number; j++) {
            var input = document.createElement("div");
            input.innerHTML = "<input type='text' style='display:inline' name='x"+i+j+"' size='4'>";
            determinante.appendChild(input);
        }
        determinante.innerHTML += "<br>";
    }
}
generarDeterminante(number);
}

This is annoying, sure it's a silly and little problem, but I don't know how to find it...

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
Suzamax
  • 50
  • 6

2 Answers2

1

See if canceling the click action helps with stopping with refreshing the page.

<button onclick="generate(); return false;">

Ideally you would use preventDefault to stop the action, but return false works just as well.

epascarello
  • 204,599
  • 20
  • 195
  • 236
1

Buttons inside of a form are typed by default to be submit. Ensure that clicking the button does not cause a form submission by typing it as a button.

<button type="button" onclick="generate()">Generar cuadrícula</button>
Community
  • 1
  • 1
Travis J
  • 81,153
  • 41
  • 202
  • 273