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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:19:06+00:00 2026-05-27T06:19:06+00:00

I have the following code which is designed to create an onchange event handler

  • 0

I have the following code which is designed to create an onchange event handler for all elements with class name ‘State’. The only problem is that I want the element to be passed into the StateChange function. How can I update this JS to pass ‘this’ into the StateChange function?

var c = document.getElementsByClassName('State');
for (i = 0; i < c.length; i++) c[i].onchange = createEventHandler( StateChange, c[i] );

Edit: I forgot to provide the createEventHandler function. Sorry about that… Here it is:

function createEventHandler(fn, input) {
      return function () {
          fn(input);
      };
}

Also, some clarification. The purpose of the function is to obviate the need to put the onchange event next to each element with class name = ‘State’. The result should be the same as if I were to write:

<select id="MyState1" class="State" onchange="StateChange(this)">
  • 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-27T06:19:06+00:00Added an answer on May 27, 2026 at 6:19 am

    Update:

    Re your updated question: You’ve said that the end result you want is as though you’d done this:

    <select id="MyState1" class="State" onchange="StateChange(this)">
    

    Your quoted createEventHandler function does exactly that.

    Original Answer(s):

    I’m not entirely sure I know exactly what you’re trying to do. I can read the question at least two ways:

    1. Inside the StateChange function call, you want this to refer to the element that changed.
    2. Inside the StateChange function call, you want this to be the same as this where you’re setting up your event handler.

    Option 1: You want this = element within StateChange

    You don’t actually have to pass the element instance into createEventHandler, because when the event occurs, this will refer to the element because of the way you’re hooking it up. But if you prefer to set it explicitly, your createEventHandler function could look like this:

    function createEventHandler(handler, element) {
        return function(event) {
            return handler.call(element, event || window.event);
        };
    }
    

    What that does is return a function that, when the event is triggered, will call the function you pass in (StateChange) with this set to the element you pass in.. This uses the JavaScript call feature of function objects, which allows you to define what this will be during the function call. You just pass it in as the first argument to call (subsequent arguments are passed on to the function being called).

    If you want to rely on the fact that the way you’re setting up the handler, this will already be set to the element instance, you can do away with the element argument:

    function createEventHandler(handler) {
        return function(event) {
            return handler.call(this, event || window.event);
        };
    }
    

    That just passes along the this value set up for the event handler by the browser.

    Option 2: You want this = this as of where you’re setting up the handler

    It’s the same principle as the above, just with a different argument. In this case, you’ll need to pass this in:

    var c = document.getElementsByClassName('State');
    for (i = 0; i < c.length; i++) c[i].onchange = createEventHandler( StateChange, this, c[i] );
    

    …and then createEventHandler looks like this:

    function createEventHandler(handler, thisArg, element) {
        return function(event) {
            return handler.call(thisArg, event || window.event, element);
        };
    }
    

    (Note I’ve passed in the element as a second argument to StateChange.)

    More reading:

    • Mythical methods
    • You must remember this
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which works fine. However, I only want to return
I have the following code (VB.NET) which is designed to determine if a given
I'm trying to use opengl in C#. I have following code which fails with
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which works just fine when the method is POST,
I have the following code which should put programs startable in Bash. if [
I have the following code which reads in the follow file, append a \r\n
I have the following code which re-uses a CookieContainer which logs in on the
I have the following code which I stripped out of any non-essential lines to
I have the following code which ends up forever reading '/proc/cpuinfo' as it keeps

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.