title: Uncaught TypeError: Cannot call method 'toString' of null
body: <p>I'm trying to create a class in JavaScript to validate a form. That'll check the form elements and validate a field if it has a specific attribute.</p>
  
  <p>However, the call to <code>getAttribute</code> isn't returning a value. It doesn't get the value inside another variable, but, if I print, it works well.</p>
  
  <p>Here's the code for my class:</p>
  
  <pre><code>function valida() {
      document.getElementById("msgDiv").innerHTML="";
      var totalErros=0; 
      var x=document.getElementById("frm1");
      for (var i=0;i&lt;x.length;i++){
          var input=document.getElementsByTagName("input")[i];
          var campo=input.getAttribute("id");
          var tipo=input.getAttribute("tipo");
          var nome=input.getAttribute("nome");
          var id=campo.toString(); //the error goes here
          //var valor=_$(id).value;
          alert(campo);
  
          switch (tipo) {
              case "obrigatorio":
                  if(document.getElementById(id).value==""){
                      document.getElementById("msgDiv").innerHTML+="Deu erro no campo "+nome+"&lt;br /&gt;"; 
                      totalErros++;}
                      break
              case "oemail":
                  if(document.getElementById(id).value==""){
                      document.getElementById("msgDiv").innerHTML+="Deu erro no campo "+nome+"&lt;br /&gt;"; 
                      totalErros++;}
                      break
              case "email":
                  if(!ValidaEmail(document.getElementById(id).value)){
                      document.getElementById("msgDiv").innerHTML+="O "+nome+" que você informou é inválido "+document.getElementById(id).value+"  &lt;br /&gt;"; 
                      totalErros++;}
                      break
              default:
                  document.getElementById("msgDiv").innerHTML+="&lt;br /&gt;";
          }            
      }
      if(totalErros==0) {  
          document.getElementById("msgDiv").innerHTML="Agora foi "+ totalErros;
          return true;
      }
  }
  </code></pre>