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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:51:49+00:00 2026-05-16T19:51:49+00:00

I have some javascript and am creating some html on the fly. What I

  • 0

I have some javascript and am creating some html on the fly. What I want to do is add an event handler to each control (they are all “Input” controls) so that if I click on any of them they all fire the same event. I first define a delegate in the initialize section of the code like this:

 this.rule_event_handler = Function.createDelegate(this, this.rule_selected);

I have a function shown below that I want all the controls to call when clicked:

 rule_selected: function () {}

I add the handler as shown below:

display_rules: function () {
    var rulearea = $('#ruleControlArea')[0];
    rulearea.innerHTML = "";
    for (intI = 0; intI < this.arrRules.length; intI++) {
        rulearea.innerHTML += this.create_rule_control(intI, this.arrRules[intI].display);
        $addHandlers($('#rule_' + intI)[0], { 'click': this.rule_event_handler }, this, true);
    }
},

The problem I then see is that only the last control cause the onclick event to fire. How can I change the code so that all the controls will fire the same event?

  • 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-16T19:51:50+00:00Added an answer on May 16, 2026 at 7:51 pm

    I’d use the .live method

    rule_selected: function () {}
    

    add a class selectable to your rules and add the live event before creating/deleting your rules items

    $(".selectable").live("click", rule-selected);
    

    If you really want to use the id of your rules, i’d look into your usage of ids; it looks like you’re using the same id many times on the page (you’re using the id query as an array, which i find strange; any specific reason for that?) and that may cause weird behavior.

    EDIT

    here is a sample

    <div id="RulesControlArea">
    </div>
    
    <div id="AddRule">Add Rule</div>
    <div id="DisplayRules">Display Rules</div>
    
    <script type="text/javascript">
    
        function display_rules() {
            var rulearea = $('#RulesControlArea');
            rulearea.empty();
            for (intI = 0; intI < 10; intI++) {
                rulearea.append("<div id='" + Math.random() + "." + intI + "' class='selectable'>this is a rule from display_rules</div>");
            }
        }
    
        $("#RulesControlArea .selectable").live('click', function(element) { alert(element.target.innerHTML); });
        $("#AddRule").click(function() { $("#RulesControlArea").append("<div id='" + Math.random() + "' class='selectable'>this is a rule</div>"); });
        $("#DisplayRules").click(display_rules);
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am mlearning javascript and have some trouble creating an onject via prototype. I
I have some php code that is creating an html table. Within it I
I am creating a form using only HTML and JavaScript where I want to
I'm creating this online store with a javascript shopping cart. Some products have options,
I have some javascript where I append an option to a select: var myuniqueddl
I have some javascript code with do while outer loop and switch inner loop,
I have some javascript applying changes to some elements and a function loading more
I have some javascript that I am using for some audio on my website:
I have some Javascript attached to the Service Activity form which fires OnLoad. It
I have some javascript that goes out and fetches a javascript class on another

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.