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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:33:22+00:00 2026-05-15T15:33:22+00:00

I have the following event: Ext.onReady(function() { Ext.select(‘.gallery-item img’).on(‘click’, function(e) { Ext.select(‘.gallery-item’).removeClass(‘gallery-item-selected’); Ext.get(e.target).parent().addClass(‘gallery-item-selected’); });

  • 0

I have the following event:

Ext.onReady(function() {

    Ext.select('.gallery-item img').on('click', function(e) {
        Ext.select('.gallery-item').removeClass('gallery-item-selected');
        Ext.get(e.target).parent().addClass('gallery-item-selected');
    });

});

Which works fine when the page loads.

However I dynamically create additional divs of the class gallery-item with a image inside them. The click event stops working once I create a new item.

How may I update this binding?

Thanks.

  • 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-15T15:33:23+00:00Added an answer on May 15, 2026 at 3:33 pm

    Ext.select selects all the elements and statically adds the click handler to them at that time. For new elements to have the same handler, it would have to also be added to them after they are created. However, this is not an optimal approach.

    It would be better to use event delegation in this case — add a single click handler to your container element, then delegate the handling based on the item that was clicked. This is more efficient (only one event handler fn required) and far more flexible. For example, if your containing element had the id ‘gallery-ct’ it would be like:

    Ext.onReady(function() {
        Ext.get('gallery-ct').on('click', function(e, t){
          // t is the event target, i.e. the clicked item.
          // test to see if it is an item of the type you want to handle
          // (it is a DOM node so first convert to an Element)
          t = Ext.get(t);
          if(t.hasClass('gallery-item'){
            // radioClass automatically adds a class to the Element
            // and removes it from all siblings in one shot
            t.radioClass('gallery-item-selected');
          }
        });
    });
    

    EDIT: If you may have nested items within your click target, you’ll want to take a slightly (but not much) more advanced approach and look for your target as the click event bubbles up from the clicked element (using EventObject.getTarget). If your target is in the event chain as it bubbles up from the clicked el, then you know it’s still a valid click. Updated code:

    Ext.onReady(function() {
        Ext.get('gallery-ct').on('click', function(e, t){
          // disregard 't' in this case -- it could be a child element.
          // instead check the event's getTarget method which will 
          // return a reference to any matching element within the range
          // of bubbling (the second param is the range).  the true param 
          // is to return a full Ext.Element instead of a DOM node
          t = e.getTarget('.gallery-item', 3, true);
          if(t){
            // if t is non-null, you know a matching el was found
            t.radioClass('gallery-item-selected');
          }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the need of a click tab event. I am using the following
I have a system-wide manual reset event that I create by doing the following:
Say I have the following: tr { background: #fff; } tr.even { background: #eee
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following situation. A main table and many other tables linked together with
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following text in a file 23456789 When I tried to replace the

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.