samedi 5 décembre 2015

Strange Javascript code behavior

I have a written HTML form with some text fields on which I need to work with Javascript. I want to select them using the method getElementsByClassName because I don't know their exact number (so I can't assign ids one by one).

 <form ...>
    <input type = "text" name = "test1" class = "myClass">
    <input type = "text" name = "test2" class = "myClass">
 </form>

 <script type = "text/javascript">
 var fields = document.getElementsByClassName("test-fields");
 </script>

Using console.log(fields[0]) writes undefined, so I am not able to iterate into the node by using a for loop (it seems like there is no element into the variable "fields", even though console.log-ging it it shows an array-like structure (as it should be). I already tried using the "form" object but the situation is the same.

Aucun commentaire:

Enregistrer un commentaire