A guide to accessing and managing events for the child inside a div element using extjs4

How can I dynamically switch between the divs with classes "icon-right" and "icon-left" when clicked? Here is an example of the HTML and ExtJS code involved:
    Html code:
        <div class="msg"> 
           <div> 
               <div class="sample"></div> 
           </div>
           <div> 
              <div class="icon-right"></div> 
           </div> 
        </div>
    Extjs code:
        var message = Ext.select('div:next(div.icon-right)');

        message.on('click', function () {
            alert('test msg');
        }, message);

I want to create functionality where clicking on a div with class "icon-right" changes it to "icon-left", and vice versa. How can this be achieved using JavaScript?

Answer №1

Would you be able to perform the following task:

Select all elements with the class 'icon-right', then move up to the nearest div element and attach a click event listener to it?

Answer №2

According to the provided information, Ext.select will yield either a CompositeElement or a group of DOM elements. It is recommended to use Ext.query which returns an HTMLElement and allows you to define the onclick event handler as follows:

var target = Ext.query(".icon-right").pop(); // retrieves the first div from the query
target.onclick = function(){ alert('clicked!'); }

If you prefer to use Ext event listeners, you should utilize the Ext.dom.Element class. An instance of this class is obtained through Ext.get, requiring an ID, DOM Node, or Existing Element as specified in the documentation. Once you have retrieved the element using Ext.get, you can then assign a listener using Ext.dom.Element.on just like in the code snippet provided.

Functional jsFiddle Example: http://jsfiddle.net/LcRWB/ (demonstrates both scenarios)

Answer №3

This solution seems promising.

let element = document.querySelector("[class=arrow-left]");
element.addEventListener('click', function() {
  console.log("Hello");
});

You may view a demonstration on codepen http://codepen.io/YvonneS/pen/jxRWyM/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create a unique command that will run regardless of the success or failure of the command before

Even if a preceding command fails, the end command is still executed. For example: browser.waitForElementIsVisible("non-existing-item", 500).end() I am looking to create a custom command that always gets called in the same way as the end command. When I t ...

How to centrally position an image within a cropped parent container using CSS

Is there a way to center an img within a parent div tag that has overflow: hidden set? I am looking to clip the image equally on both sides in order to display the middle portion. <div class='wrapper'> <img/> </div> The desi ...

Is there a way to eliminate the outline on a FormControl component in React-bootstrap when the input field is selected?

Currently, I'm working on creating a search bar with dropdown suggestions using React-bootstrap. My goal is to have the search bar span across the page from one side to another with margins on the left and right. To achieve this, I am enclosing the in ...

Store the output of the function in a variable

Here is a script that was provided to me: <div id="container1"> <script> var script = document.createElement("script"); script.type = "text/javascript"; script.text = 'document.write(xmlDoc.getElementsByTagName("INFO") ...

The `@import` statement fails to load styles even when the URL is perfectly accurate

I am attempting to implement CSS styles. @import url("http://mydomain.com/theme/css/reset.css") However, it seems that the styles are not being applied. When I access through the browser, I can see all the CSS rules loading. What am I doing incorrectly ...

When the page is loaded, populate FullCalendar with events from the model

On page load, I am attempting to populate events with different colors (red, yellow, green) on each day of the calendar. Here is a simple example showcasing events for three days: https://i.sstatic.net/YzJ4E.png I have data in a model that indicates the ...

In a perplexing twist, requests made to the Express app arrive with empty bodies despite data being sent, but this anomaly occurs

Welcome to the community of inquisitive individuals on Stack! I'm facing an interesting challenge while developing an Express app. Despite everything running smoothly with two routes, I've hit a roadblock with one route that seems to have empty i ...

Having trouble with tabs in jQuery?

I'm having trouble setting up tabs in a reservation form with 3 tabs that include text boxes for user input. I can't seem to get it working properly and I'm not sure where I've gone wrong. Could it be due to the placement of the content ...

Intensive analysis of objects comparing their characteristics

Currently delving into the world of Javascript, I encountered a coding exercise involving a "deepEqual" function that I attempted to write but ultimately struggled with. Even after reviewing the solution, one particular part perplexed me - the for loop, ...

Display the personalized list of user items on the MERN dashboard

I'm currently developing a React booking platform that interacts with my backend through a Rest API using axios and redux. My challenge now is to display personalized reservations and rooms for each user on the website. However, I'm facing an iss ...

Responsive div that reacts to scrolling

I am looking for a div that can dynamically change its position as I scroll up or down the page. For example, it could start 30px from the top but then move to 100px as I scroll down further. This div will be located on the right-hand side of my page, clo ...

Developing a fresh instance in JavaScript

Currently, I am working with a JSON object that I'm required to use in my project. const template = require('../../../../assets/jsons/article-tpl.json'); Next, I need to iterate through an array and utilize this object to generate a new te ...

Efficient method for managing complex JSON object updates using setState in React

My task involves handling structured data in JSON format, which I am unable to modify due to API restrictions. The challenge is to update the JSON file based on user modifications. { "id": 1269, "name": "Fet", &quo ...

The function in an AngularJS controller is failing to execute

I am experiencing an issue with calling the checkLogin function of a controller. The value returned by checkLogin is used to show/hide elements with ng-show. However, when debugging, I noticed that the execution stops after the function call and does not p ...

Animating CSS when closing a modal box

I followed the instructions from a tutorial on W3Schools here to create this code. The "open model" button triggers the modal to open with a smooth CSS animation, which looks great. However, when I click the close button, the modal abruptly closes without ...

Why is Selectpicker failing to display JSON data with vue js?

After running $('.selectpicker').selectpicker('refresh'); in the console, I noticed that it is loading. Where exactly should I insert this code? This is my HTML code: <form action="" class="form-inline" onsubmit="return false;" me ...

Creating a static file for deployment: A step-by-step guide

Currently, my node and webpack configuration allows me to run the dev-server and work on my application. However, I am facing issues in generating the static bundle.js file required for deployment on my website. I need help configuring my webpack.js file ...

Creating a triangle with SVG polygon: A step-by-step guide

I'm attempting to create a bottom triangle using SVG polygons, similar to the image below: https://i.stack.imgur.com/571FM.png Here is what I have tried so far: <svg xmlns="http://www.w3.org/2000/svg" height="150px" width="100%" viewBox="0 0 ...

Implementing a post request triggered by a button click in Node.js with Express

How can I invoke a controller from a button click event? I tested it in Postman and it works fine, but I'm having trouble calling it from a button on my front-end. I am using Nodemailer and Node Express to send emails. Here is my code. Can someone p ...

How to save HTML content in a variable for future use in Next.js

When working with Next JS, my code resembles something like this: function Test() { return ( <section> <div>Test</div> </section> ); } Now, imagine that I want to have multiple entries here, gen ...