getElementById doesn't work in Firefox or Macintosh browsers

Posted on 2005-04-20
Last Modified: 2008-02-07
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");
            return false;
                 else { return true; }

I have to use getElementById due to the name of the variable starting with a string (solved yesterday:

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.
Question by:djs120
    LVL 25

    Accepted Solution

    try this:
    LVL 1

    Author Comment

    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.
    LVL 25

    Expert Comment


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

    <form action= onsubmit="return validForm(this)">
          <input type=text name=01name value="">
          <input type=submit>


    simple and less code ;)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
    This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now