• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6895
  • Last Modified:

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: http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21394962.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.
0
djs120
Asked:
djs120
  • 2
1 Solution
 
devicCommented:
try this:
===============
passForm.elements["01name"].value
0
 
djs120Author Commented:
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.
0
 
devicCommented:
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 ;)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now