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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:54:35+00:00 2026-05-12T11:54:35+00:00

I have a composite control which emits HTML that looks like this <span id=myControl>

  • 0

I have a composite control which emits HTML that looks like this

<span id="myControl">
   <select id="myControl_select">
      <option value=""></option>
      <option value="1">Item 1</option>
      <option value="2">Item 2</option>
   </select>
</span>

I would like the parent span control to fire an event when the contained drop down list onchange event fires. Since this is a composite control, I have to allow a consumer of this control to be able to assign the event handler (I was hoping via an html attribute called onvaluechanged or something)


Editted to incorporate bobince’s suggestion. The only problem is the argument parameter is not being passed. It’s currently ‘undefined’.

<script>
    window.onload = function() {
        var objSelect = document.getElementById('myControl_select')

        if (objSelect.attachEvent)
            objSelect.attachEvent("onchange", myControl_ddlOnChange);
        else if (objSelect.addEventListener)
            objSelect.addEventListener("onchange", myControl_ddlOnChange, false);
    }   

    function myControl_ddlOnChange()   
    {
        //fire span on value changed event   
        var target= document.getElementById('mycontrol');
        var handler= target.getAttribute('onvaluechanged');

        var args = { text : 'Hi!', index : 0 } 

        if (handler)    
            new Function(handler).call(target, args);
    }

    function myControl_ValueChanged()
    {
        alert(arguments[0]);
    }
</script>

<span id="myControl" onvaluechanged="myControl_ValueChanged();">
   <select id="myControl_select">
      <option value=""></option>
      <option value="1">Item 1</option>
      <option value="2">Item 2</option>
   </select>
</span>
  • 1 1 Answer
  • 3 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-12T11:54:35+00:00Added an answer on May 12, 2026 at 11:54 am

    Don’t use attachEvent, it’s IE-only. You can hack up solutions that use either attachEvent on IE or addEventListener on other browsers, or use a framework that already does that for you, but for your purposes, plain event handler properties should be enough:

    document.getElementById('myControl_select').onchange= myControl_ddlOnChange;
    

    It’s not really a good idea to start adding your own custom HTML onsomething attributes. Apart from being invalid, the browser won’t do that magic it does to turn an onclick HTML attribute into an onclick JavaScript property. You’d have to do that manually, firing like:

    var target= document.getElementById('myControl');
    var handler= target.getAttribute('onvaluechanged');
    if (handler)
        new Function(handler).call(target);
    

    It’s generally better to have the caller write to ‘onclick’ with a JavaScript function themselves. HTML event handler attributes pretty much suck and should generally be avoided.

    ETA re comment:

    The reason no arguments are being passed is because your event handler:

    onvaluechanged="myControl_ValueChanged();"
    

    doesn’t pass on any arguments. You could say:

    onvaluechanged="myControl_ValueChanged(arguments[0]);"
    

    or, to pass on any number of arguments:

    onvaluechanged="myControl_ValueChanged.apply(this, arguments);"
    

    But really this is getting away from how event handlers normally work, since they don’t have arguments(*). Much easier to just stick with JavaScript and say:

    var control= document.getElementById('myControl');
    
    // to register
    //
    control.onvaluechanged= myControl_ddlOnChange;
    
    // to call
    //
    control.onvaluechanged(/* any arguments; 'this' inside ddlOnChange is the control */);
    

    rather than this curious Function stuff.

    Other minor nits: addEventListener takes ‘change’ rather than ‘onchange’; capital C in getElementById(‘myControl’).

    (*: except for the ‘event’ argument, in non-IE)

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

Sidebar

Ask A Question

Stats

  • Questions 197k
  • Answers 197k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The C++98 standard says in section 3.6.1 paragraph 2 An… May 12, 2026 at 7:18 pm
  • Editorial Team
    Editorial Team added an answer MySQL will always have a mysql database to store user… May 12, 2026 at 7:18 pm
  • Editorial Team
    Editorial Team added an answer l = ("xxxxxxxxx", "yyyy","zz") print(max(l, key=len)) First of all you… May 12, 2026 at 7:18 pm

Related Questions

As I understood , The property grid is given an object which it can
I'm designing a new component to a system, trying to follow the various guidelines
I have a composite control on an ASPX page. There is Javascript on the
I have a composite control that adds a TextBox and a Label control to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.