Necesito ayuda...(i need help) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Necesito ayuda...(i need help)

Necesito ayuda para saber dónde está el error en el codigo de Javascript, ya intenté varios métodos pero no he podido hacer que funcione, se supone que el codigo debe ejecutar un recorrido para que el ID en Css se quede en -85 px en caso de que haya texto dentro, pero me sale error al intentarlo con getElementByClassName por eso lo puse en .....ById y con eso me tira error en el for ya que dice que i > inputs.length; no es válido. I need help to find out where the error is in the Javascript code, I already tried various methods but couldn't get it to work,The code is supposed to run a loop so that the ID in Css stays at -85 px in case there is text inside, but I get an error when trying with getElementByClassName that's why I put it..... ById and with that it throws an error in the for since it says that i> inputs.length; It's not valid. https://code.sololearn.com/WJgyUnHgyRcK/?ref=app https://code.sololearn.com/WJgyUnHgyRcK/?ref=app

14th May 2021, 8:12 AM
Ángel De Jesus Soto Herrera
Ángel De Jesus Soto Herrera - avatar
4 Answers
+ 1
Your problem is, that you try to access the DOM before it exists. You need to make a window.onload function to start the code. Otherwise it wont find anything. And not getElementByClassName but getElement**s**ByClassName. I made the same mistake while i was beginning to program
14th May 2021, 5:37 PM
Nico Ruder
Nico Ruder - avatar
+ 1
Ángel De Jesus Soto Herrera Try This: window.onload = function() { var inputs = document.getElementsByClassName('formulario_input'); for(var i = 0; i > inputs.length; i++) { inputs[i].addEventListener('keyup', function () { let cL = this.nextElementSibling.classList; if(this.value.length >= 1) { cL.add('fijado'); } else { cL.remove('fijado'); } }); } } I changed some things and edited the getElementsByClassName
15th May 2021, 5:26 AM
Nico Ruder
Nico Ruder - avatar
+ 1
Hey, no problem, you can also write me privately and ask me something
15th May 2021, 4:46 PM
Nico Ruder
Nico Ruder - avatar
0
Nico Ruder, tnx I really love you
15th May 2021, 6:19 AM
Ángel De Jesus Soto Herrera
Ángel De Jesus Soto Herrera - avatar