javascript - Parent or ParentNode / Class Selector -


class selector not working , how class apply function in particular , not first, because items have same class if selector class work take first class , not in element i'm applying.

html

<li class="space_form">     <label>         nome*         <input type="text" class="no-empty" name="nome" value="nome" onfocus="formemptyleft(this);cleartext(this)" onblur="formemptyleft(this);cleartext(this)" />         <div class="campo_vazio-left">*preenchimento campo obrigatório</div>                                                                                          </label> </li>  <li class="fright">     <label>         sobrenome*         <input type="text" class="no-empty" name="sobrenome" value="sobrenome" onfocus="formemptyright(this);cleartext(this)" onblur="formemptyright(this);cleartext(this)" />         <div class="campo_vazio-right">*preenchimento campo obrigatório</div>                                                                                                                                         </label> </li> 

javascript

function formemptyleft() {     var input = document.getelementbyclass('no-empty');      if(input.value.length == 0)     {         document.getelementbyclass('campo_vazio-left').style.display = 'block';          document.getelementbyclass('no-empty').classlist.add('form_invalido');                   }     else {         document.getelementbyclass('campo_vazio-left').style.display = 'none';          document.getelementbyclass('no-empty').classlist.remove('form_invalido');                        }        }     function formemptyright() {     var input = document.getelementbyclass('no-empty');      if(input.value.length == 0)     {         document.getelementbyclass('campo_vazio-right').style.display = 'block';          document.getelementbyclass('no-empty').classlist.add('form_invalido');                   }     else {         document.getelementbyclass('campo_vazio-right').style.display = 'none';          document.getelementbyclass('no-empty').classlist.remove('form_invalido');                        }        }    

the javascript code added in your other question incomplete - @ least chrome console tells (see screenshot).

overview of errors

<input type="text" class="no-empty name_field" name="nome" value="nome"        onfocus="formemptyleft();cleartext()"       onblur="formemptyleft();cleartext()" />  

above cleartext() function fired - code above has no cleartext function.

in second screenshot see result of these lines

var input = document.getelementsbyclassname('no-empty'); if(input.value.length == 0) 

you forgot add document.getelementsbyclassname('no-empty')[0]; , have array instead of input element.

error location

here can see document.getelementsbyclassname('no-empty'); returns array. forgot add [0] access first element of array.

result of code returns array


Comments