Link to home
Start Free TrialLog in
Avatar of djs120
djs120

asked on

getElementById doesn't work in Firefox or Macintosh browsers

I have the following HTML:

<intput type="text" name="01name">
(and it is required to have the 01 in front for a server side order processing script to run.

I have a JS function that is being called onSubmit:

function validForm(passForm) {
      if (document.getElementById('01name').value == '') {
            alert("You must enter a name");
            document.getElementById('01name').focus();
            return false;
      }
......
                 else { return true; }
}

I have to use getElementById due to the name of the variable starting with a string (solved yesterday: https://www.experts-exchange.com/questions/21394962/Need-to-get-value-of-variable-that-starts-with-a-number-ex-01email.html)

But this code works in IE on PC, but not on Firefox (PC or Mac) or Safara (mac) or IE (Mac).  Is there some issue with getElementById?  I also tried getElementsByTagName but that doesn't work either!

The error I get in Firefox is:
Error: document.getElementById("01name") has no properties

Please help!  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of devic
devic
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of djs120
djs120

ASKER

Actually, I feel really stupid because the problem is a lot simpler than I thought, but a lot of people have had the same problem from my googling, so here's the solution, at least for me:

in the input tag:
<intput type="text" name="01name">

notice I have no id="01name" and yet I'm trying to use the JS getElementById method, and of course it can't find anything with that ID.  So I added it as:
<intput type="text" name="01name" id="01name">

I have no idea why it worked on IE and nothing else, but I am guessing it's due to some relaxed conformance with IE.

Thanks for the help, you get the points since I figured it out too late to delete this.
djs120,

for forms I use always form object and i believe it's the best choise:
===================================
<script>
function validForm(passForm)
{
      var el=passForm.elements;
      if(el['01name'].value == '')
      {
            alert("You must enter a name");
            el['01name'].focus();
            return false;
      }
      else { return true; }
}
</script>

<form action=http://sembel.net/tools/q.php onsubmit="return validForm(this)">
      <input type=text name=01name value="">
      <input type=submit>
</form>


=============================

simple and less code ;)