Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 762581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:25:00+00:00 2026-05-14T16:25:00+00:00

I have the following JavaScript in my HTML page referencing an HTML form on

  • 0

I have the following JavaScript in my HTML page referencing an HTML form on the page:

<script type="text/javascript">
<!--

var myForm = document.myForm;

function validateForm() {
    if (myForm.myInput == "")
        alert("Please input some text.");

        return false;
    }

    myForm.submit();
}

function showFormInput() {
    myForm.reset();

    document.getElementById('myInput').style.display = 'inline';
}

//-->
</script>

...

<form name="myForm" id="myForm" action="..." method="post">
    <input id="myInput" name="myInput" type="text" value="" style="display:none;" />
</form>

Both functions are throwing an exception when trying to access the variable myForm, saying that “myForm is null or not an object”. Why is this occurring?

UPDATE: One thing I think I’m gathering from this is that global variables should generally be used for string literals – not elements in the DOM. I”m going to go forward with this, and use element variables sparingly, and only after the DOM has been loaded.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T16:25:00+00:00Added an answer on May 14, 2026 at 4:25 pm

    What browser are you using?

    IN general you should never use document.* access. This is an MS-IE convention. Instead, use

    var myForm = document.getElementById( 'myForm' ) ;
    var myInput = document.getElementById( 'myInput' ) ;
    
    if( myInput.value == '' )
    {
        // its empty!
    }
    

    As outis noted, either put your script block at the bottom of your page, or use the onload event of <body> tag, like this

    <script>
    function go()
    {
        alert( "The DOM has been assembled and can be accessed.. nOW!!!" ) ;
        var myForm = document.getElementById( 'myForm' ) ; // ...
    }
    </script>
    <body onload="go() ;">
    </body>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following, very simple html page: <html> <head> <script type="text/javascript"> function alertSelection()
I have the following html in page: <head> <script src=jquery-1.3.2.min.js type=text/javascript></script> <script> function hideIt()
I have the following code on an html page: <script type=text/javascript> <!-- window.location.replace(http://www.example.com/); -->
I have the following HTML page: sample_page.html: <h1>sample html page</h1> <script type=text/javascript src=sample_page.js/> The
I have the following HTML page: <html> <head> <script type=text/javascript src=JavaScript/Menu.js></script> </head> <body> <ul>
I have HTML which includes scripts in following order <script src=../static/js/jquery.js type=text/javascript> <script src=../static/js/bootstrap.js
I have the following HTML containing a drop down list (single selection) <script type="text/javascript">
I have the following javascript into a index.php file: <script type=text/javascript> $('.showpics, .showpics2').live('click', function()
I am referencing JavaScript as follows on an HTML page: <script type=text/javascript src=http://code.jquery.com/jquery-1.6.1.min.js></script> <script
I have the following Javascript code: function checkIfValid(){ var form = document.createuserform; if(form.fName.value ==

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.