I have the following code:
$(document).ready(function(){
console.log($("body").find("form label input")); //da un objeto, una lista, con los diferentes inputs dentro del label dentro del form
for (var i = 0; i < $("body").find("form label input").length; i++) {
console.log($("body").find("form label input")[i]); //repite cada uno de los objetos seleccionados por orden
$("body").find("form label input")[i].click(function(){ //parece ignorar la creación del disparador o eventListener.
alert("¿hola?"); //no se ejecuta el alert
});
}
}
Why isn't an eventListener created for each input on each iteration of the loop? I don't understand...
You can create a
Listener
directly fromJavaScript
. via addEventListener() as follows:Although from Jquery it would have been enough to
With jQuery it's very easy, you just have to identify which elements you want to click and assign them in jQuery selectors that are the same as CSS.
This way you avoid going through cycles in the DOM objects because when you go through them you also go through the functions and other methods that it has, that is, you would consume less PC resources.