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

  • SEARCH
  • Home
  • 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 6829967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:33:46+00:00 2026-05-26T22:33:46+00:00

My myclickHandler function works perfectly but when I go on to my error console

  • 0

My myclickHandler function works perfectly but when I go on to my error console I get this error:

document.getElementsByName(“prequestion”)[0] is undefined;

Below is my code

<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <title>Create a Session</title>
<script type="text/javascript">

    document.getElementsByName("prequestion")[0].addEventListener('click', myClickHandler);

    function myClickHandler(){
         if(validation()){
            openSessionPopup();
         }

  </script>
    </head>

<body>

<form action="create_session.php" method="post" name="sessionform">

<p><strong>10: </strong><input class="questionBtn" type="button" value="Prepare Questions" name="prequestion" onClick="myClickHandler()"/></p>   

</form>
</body>

My question is what do I need to remove this error? because my function works does it matter if it shows an error in the error console?

  • 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-26T22:33:46+00:00Added an answer on May 26, 2026 at 10:33 pm

    Chances are you’re loading the javascript before the DOM (or even possibly the HTML) has loaded properly.

    For example, the following reproduces the same error as described in your question :

    <script type="text/javascript">
    document.getElementsByName("prequestion")[0].addEventListener('click', myClickHandler);
    
    function myClickHandler(){
         alert('test');
        }
    </script>
    <input type="" name="prequestion" />
    

    Whereas the following does not. The error is not present and the code works fine.

    <input type="" name="prequestion" />
    <script type="text/javascript">
    document.getElementsByName("prequestion")[0].addEventListener('click', myClickHandler);
    
    function myClickHandler(){
         alert('test');
        }
    </script>
    

    Trying to attach the event listener to an element which doesn’t technically exist yet causes problems. You can check for the browser reporting the page has been loaded before attaching the event if needs be but be aware that browsers report this in different ways. Librarys such as jQuery provide useful means to test this such as $(document).ready(function(){});

    Edit: I’ve just seen that you’ve added the relevant HTML too. It appears that this is in fact the case and the reason you’re still seeing the expected behavior is because you’re also using the onclick="" attribute on the input element. This is where the function is being called on click, and the event handler isn’t being attached as you expect.

    Edit #2: You also appear to be missing a closing } for your function. And I’ve now tested with your exact HTML and moving the script to just above the body tag resolves the error and correctly attaches the function to the click event.

    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <title>Create a Session</title>
    </head>
    
    <body>
    <form action="create_session.php" method="post" name="sessionform">
    <p><strong>10: </strong><input class="questionBtn" type="button" value="Prepare Questions" name="prequestion" /></p>   
    </form>
    <script type="text/javascript">
        document.getElementsByName("prequestion")[0].addEventListener('click', myClickHandler);
    
        function myClickHandler(){
             alert('test');
        } // This was missing
      </script>
    </body>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below I have a Json function and some php code which it retrieves. JavaScript
I am using PHP and I return this value: $_SESSION['sessionNum'] and I get an
Below is my code for a simple form in the create_session.php page. I am
I'm looking to add a click handler to a div. But is there a
My AS3 application basically does the following (pseudo code from main application class) 3
I'm a bit confused, I set up my app as a simple converter but
Below I have an application. Now on the previous page user types in a
The is my code.I am here pupulating a list view with some array data.when
I have this layout in xml. And when I run the activity at the
I am basically doing a tutorial but my program seems to have errors that

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.