Mark mark in jade does not work

0

I have the following code that dynamically marks what the user writes, note that pure html is used, but when it happens to jade it does not work

var normal,
        destacado,
        texto,
        entrada;

    function inicializar() {
        
        normal = document.getElementById("normal");
        destacado = document.getElementById("destacado");
        texto = normal.innerText;
        entrada = document.getElementById("captureInput");
        entrada.addEventListener('input', actualizar);
    }

    function actualizar() {

        var porDestacar = entrada.value,
            pos = porDestacar.length;

        //Asignar la cantidad de caracteres ingresados desde el texto original
        destacado.innerText = texto.substr( 0, pos);
        //Asignar el resto
        normal.innerText = texto.substr( pos );

        //Coincide perfectamente?
        if (porDestacar === texto) {
            destacado.classList.add("completo");
        } else {
            destacado.classList.remove("completo");
        }
    }

    window.onload = inicializar();
#normal {
        background-color: transparent;
    }

    #destacado {
        background-color: yellow;
    }

    #destacado.completo {
        background-color: lightgreen;
    }
<div>
    <p>
        <mark id="destacado"></mark><mark id="normal">no existe un rayo muy fuerte</mark>
    </p>
</div>
<input id="captureInput" type="text">

and when I pass it to jade it does not work anymore

div(id="texto")
    p 
      mark(id="destacado") 
      mark(id="normal") no existe un rayo muy fuerte
    p
    textarea(type="text", id="captureInput", style="width:100%", cols="100", rows="3", placeholder="Escribe el texto de arriba en este cuadro", required)'
    
asked by hubman 23.12.2016 в 17:11
source

1 answer

1

The error is here:

window.onload = inicializar();// asignas el resultado de llamar a la funcion 'inicializar'

You are calling the function inicializar and you are assigning the result of this to the function window.onload .

The problem occurs if the script is executed before the HTML has been loaded, since the elements that are required (for example: normal = document.getElementById("normal"); ) do not yet exist.

Solution:

window.onload = inicializar;// asignar la función para que se ejecute en el onload
    
answered by 23.12.2016 / 17:23
source