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 9105737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:10:28+00:00 2026-06-17T02:10:28+00:00

I seem to be having scope issues with using addEventListener inside of an immediate

  • 0

I seem to be having scope issues with using addEventListener inside of an immediate anonymous function.

Inside of the event listener, I am creating an instance of a Javascript “class” that is constructed outside of the immediate function (using a constructor and prototype).

In trying to keep with good practices, I wanted to use the immediate function to avoid global variables, and addEventListener to avoid using inline Javascript (thus separating it from html).

So how can I correctly do this without having scope issues, and what are good practices for doing so?


Here is my html:

<html>
    <head>
        <title>asdf</title>
    </head>
    <body>
        <input type = "text" id = "userInput" />
        <input type = "button" id = "submitButton" value = "submit" />

            <script type = "text/javascript" src = "asdf.js" />
    </body>
</html>

…and here is my Javascript asdf.js (which is not working):

// constructor for object-oriented processing of the user input
function Statement(expression)
{
    this.expression = expression;
}
    // instance method
    Statement.prototype.checkSyntax = function()
    {
        var newExp = this.expression;

        return newExp;
    };

// immediate anonymous function
(function()
{   
    var submitButton = document.getElementById("submitButton");

    // event listener for the submit button
    submitButton.addEventListener("onclick", function(event)
    {
        var userInput = document.getElementById("userInput");

        // creating a new object with constructor above
        var expr = new Statement(userInput);

        // calling instance method of the "class" Statement
        alert(expr.checkSyntax() );

        // disable the button after first use
        event.stopImmediatePropagation();
    }, false);

})(); // end of immediate anonymous function
  • 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-06-17T02:10:29+00:00Added an answer on June 17, 2026 at 2:10 am

    addEventListener takes an argument like click, not onclick. However, older IE’s attachEvent requires onclick. Everything else seems fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seem to be having some issues storing the current date in a core data
I am new to JavaScript and I am having trouble understanding the scope of
CoffeeScript newbie here. I'm having a weird problem with the scope of a function
I'm having some scope issues when dot sourcing powershell scripts. Suppose I have one
I seem to be having a problem removing the image in UIScrollView. I was
I seem to be having a problem checking if a string exists in my
I seem to be having trouble with my code. I need to say: if
I seem to be having a very frustrating time with an inherited class calling
I seem to be having a problem with checking for list equality. In my
I seem to be having a problem with my footer growing to 100% width

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.