Switch the class of the parent div when clicked (or when ng-clicked)

I am attempting to create collapsible panels with a resize button by incorporating the following JavaScript code:

function toggleSize() {
    $(".elementGraphic").click(function () {
        $(this).toggleClass("col-md-12");
        $(this).toggleClass("col-md-6");
    });
};

The corresponding HTML code looks like this:

<div class="col-md-12 ui-state-default elementGraphic">
    <div class="panel-group">
        <div class="panel panel-default">
            <div class="panel-heading ">
                <h4 class="panel-title">
           <a data-toggle="collapse" href="#collapse1" class="component-button max-min-button more-less glyphicon glyphicon-chevron-up"></a>
                   <a href="#" class="component-button resize-button glyphicon glyphicon-resize-small" onclick="toggleSize.call(this)"></a>
                   Graphic 1
                </h4>
            </div>
            <div id="collapse1" class="panel-collapse collapse in element"><canvas class="chart" id="myChart"></canvas></div>
        </div>
    </div>
</div>

Unfortunately, I am facing issues with this code. There are times when multiple clicks are required for resizing and sometimes the chart inside does not resize along with the div element. I aim to find a solution that works uniformly across all elements using classes instead of IDs.

If needed, I can explore integrating AngularJS as well.

Your assistance is greatly appreciated.

Answer №1

Here's a handy snippet for you to use:

$(".graphic-element").each(function() {
  let container = this;
  $(this).find(".resize-button").click(function() {
    $(container).toggleClass("col-md-12");
    $(container).toggleClass("col-md-6");
  });
});

This code snippet helps in locating the resize button for each container and adds a click event that modifies the container itself.

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

howler.js resumes playing sprite sound after being paused

I have been working on setting up an audio sprite using Howler.js. The basic functionality of the sprite is working well, but I am facing an issue when trying to resume playback after pausing a sprite. When I call play(), it does not work as expected. sou ...

Emphasize the most recent file during the document upload process and ensure the scroll bar moves accordingly to the document

I am currently working on a feature where the scroll bar should move to the newly uploaded document in a list of documents. When I upload a new document, I want the scroll bar to automatically move to that document. Currently, the highlighting changes to t ...

Error in AngularJS: Attempting to pass a Promise, but encountering an issue with reading the property 'finally' due to it

Within my Angular application, I have two methods: save() and saveTriggers(). The saveTriggers() method is responsible for updating all records by making a call to a C# web service. My goal is to ensure that a specific block of code executes after all reco ...

What is the method for iterating through rows using csv-parser in a nodejs environment?

I'm working on a script to automate the generation of code for my job. Sometimes I receive a .csv file that contains instructions to create table fields, sometimes even up to 50 at once! (Using AL, a Business Central programming language.) Here is th ...

What could be the reason for operators like tap and map not being invoked on the inner observable when combineLatest is used?

Can you clarify why the operators tap and map of inner observable are not being called? Shouldn't combineLatest subscribe to the observables it receives in obsArr? Why are these operators not triggered by this subscription? const obsArr = []; [[1, 2 ...

Ways in which elements can be toggled through jquery or javascript?

There are various words listed below: word1 word2 word3 ... Every word in the list is linked to 1 to 3 examples. When a user clicks on a word, certain actions should take place. I want the examples to display when the associated word (e.g., word1) is c ...

Searching for the position of different size values according to their specific value

information = { boxNoTo: 1, boxNoFrom: 1, size: 'M', } items = [{ size: 'M', },{ size: 'M', },{ size: 'S,M,L,XS', boxNoTo: 1, boxNoFrom: 1, country: 'CA', name: 'Josh' }] This is what I have don ...

Utilizing jQuery or JavaScript to convert a PHP return value into an array for use in JavaScript

Is there a way to retrieve a JavaScript array from a PHP return value? For example, if a PHP script called 'makeArray.php' returns "first", "second", "third" (without the single quotes), how can I use this array in JavaScript to alert "second"? ...

I prefer to disable the toggle feature if the target remains unchanged

My goal is to create a step-by-step ordering system using the data-id="x" attribute to determine which content should be visible when selected. I want to make sure that if two elements have the same data-id, clicking on one will deselect the other. Any su ...

Establish boundaries for D3.js circle reports

I am currently working on a visualization project where I want to arrange cells with higher values to appear towards the top and left, similar to a gravity force. However, I am facing difficulties in keeping multiple circles within the boundaries of the re ...

How can I differentiate between an unreachable server and a user navigating away in a $.ajax callback function?

Situation: You have a situation where several $.ajax requests to the server are still in progress. All of them end with xhr.status === 0 and xhr.readyState === 0. Possible reasons for this issue: The server might be down (EDIT: meaning it is unreachabl ...

What is the best way to add a blob to the document object model (

I am a beginner when it comes to working with blobs, and I am looking for some guidance to avoid wasting hours on unsuccessful brute-force attempts. I have been using the PHP code below (sourced from here) to retrieve the base64-encoded image from my data ...

Issue: Anticipated the primary reducer to be a function, but was presented with: 'undefined' in Vanilla JS with Redux

Exploring the world of Redux, I decided to try out some code from a tutorial on YouTube. Building a simple Vanilla JS App, I integrated Redux into it. However, upon running the app in the terminal, an error message popped up saying: "Error: Expected the ro ...

Tips for extracting form data from a directive in Angular

I am using this template setup: <div class="modal" id="popupModal" tabindex="-1" role="dialog" aria-labelledby="createBuildingLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <d ...

When using .map() to iterate through an array of objects in Next.js, why does the data display in the console but

I'm facing an issue with displaying the elements of an array in HTML. I'm fetching data from the Bscscan API and while I can retrieve data successfully from the first API, the second one doesn't display the data in the local browser. I' ...

The challenge of integrating Bootstrap with Mixitup filtering on page load

Looking to showcase our portfolio in bootstrap using MixItUp v3.1.11 filtering, and attempting to load a specific category (not all projects) upon page load. Patrick Kunka has shared an example of achieving this here. A related question was asked here O ...

Should the use of "onClick={}" be considered poor practice?

Recently, I discovered that the use of onclick is not recommended in HTML. Currently, I am immersing myself in a React tutorial where they utilize <button onClick={shoot}>Take the Shot!</button>. I'm curious if this practice applies to Rea ...

Using a carousel component in Bootstrap

Just starting out with this, trying to customize Bootstrap to change slides automatically. I followed the documentation at https://getbootstrap.com/docs/4.3/components/carousel/ but for some reason, the slides aren't changing on an interval, even thou ...

The moment a file is uploaded from an HTML page, control is passed on to the servlet

Trying to incorporate file upload functionality in my application, I have implemented the following code in my HTML: <form action="http://localhost:8080/demo/Upload/a" method="post" enctype="multipart/form-data"> <input type="text" name="descript ...

Building Individual Elements in Angular 2

My task involves creating two distinct components in Angular 2, the Users component and the Clients component. These components are not nested and do not have any relationship. To call these components separately, I typically use main.ts as the main entry ...