How can I apply a custom class in Angular 2, similar to jQuery's

I've been having trouble with a very basic task... I need to assign a class to a <div> when I click on a button. After hours of searching online, I still haven't found a simple solution and all the examples I come across are too complex for my needs.

What I want to do is as straightforward as this (jquery) code in angular2/typescript:

 $(".button").click(function() {  
    $(".element").addClass("active");      
  });

In my component folder, I have the usual html, scss, and ts files. This functionality doesn't need to be global or anything complicated.

It's frustrating how something so simple has become difficult to achieve with modern frameworks. Any assistance would be greatly appreciated!

Answer №2

Understanding this concept is simpler than you think: the key is knowing where and what to search for.

Here is one solution:

<div [class.myClass]="isClicked"></div>
<button (click)="isClicked = true">Click me</button>

Another approach:

<div #myDiv></div>
<button (click)="myDiv.className='myClass'">Click me</button>

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

Loading more pages will be triggered by each click with JQuery's .load() function

Utilizing JQuery has been my go-to solution for various challenges in life, including page loading through the use of the .load() function upon clicking a link with a specific class. Take a look at the following code snippet: $('.pageloader'). ...

Applying custom CSS to individual divs based on their text height using jQuery

I have a grid consisting of multiple boxes each sized at 280px by 280px, and I am seeking a way to vertically align text within hover overlays on each box. While my current code successfully aligns the text for the first box, I am encountering issues due ...

How can we stop the page from scrolling after a click?

Upon clicking the link labeled Click Me, the page automatically scrolls back to the top. This behavior is not desired. How can I correct this issue? For example, here is a link to a relevant resource: http://jsfiddle.net/Y6Y3Z/ Regarding the scroll-bar: ...

In Angular2, the derived class can inherit decorators

Within my Angular application, I am utilizing the BaseComponent which has a specified template. My goal is to use this same template HTML in a component that extends the base one. However, I am aware that class inheritance in Angular2 only applies to cla ...

Unable to initialize default values on Form Load in Angular Reactive Forms

My goal is to make an HTTP request by passing an ID, then take the response and assign the values to form fields. I attempted using setValue and patchValue methods, but they did not yield the desired results. spService itemData = new ItemData() getIte ...

Using jQuery to convert JSON data into an array

My JSON data structure is as follows: { "default": [ [ 1325876000000, 0 ], [ 1325876000000, 0 ], [ 1325876000000, 0 ], [ 1325876000000, 0 ] ], "direct": [ [ 13 ...

Upgrading from Angular 5 to 6: Embracing the RxJS Changes without the crutch of rxjs

Currently, I am facing the challenging task of migrating a project from Angular 5.2.11 to version 6.0.0. The main issue I'm encountering is with RxJS 6 (which is essential for Angular versions above 6). Here's an example of one of the errors that ...

Steps for invoking a Node.js function from a distinct JavaScript function on the front end

I am currently working on a project that involves a node js backend (app.js) connected to an HTML, CSS, and Javascript frontend (script.js). Within my HTML file, I have a form that allows users to upload an image, triggering a function in script.js to han ...

Show a Toast in React without relying on useEffect to manage the state

I have successfully implemented the Toast functionality from react-bootstrap into my application using the provided code. However, I am unsure if it is necessary to utilize useEffect to set show with setShow(items.length > 0);. Would it be simpler to ...

Most efficient method of retrieving the properties of an object that may possibly contain another object within it

Having the following schema: export type PersonType = { id: string, cnp: string, name: string, surname: string, education: string, email: string, experience: string, homeCounty: string, neighboringCounty1: string, ne ...

Utilizing JQuery to extract particular user information from an HTML webpage

I am having trouble retrieving the specific username information from the links provided below. My goal is to store the username information in the variable called username when a link is clicked. Unfortunately, the current setup is not functioning as ex ...

Error: The function child.send is not defined as a valid function

Encountering an issue while attempting to build my Angular application. npm run build All tasks run smoothly on local machine and the project is successfully built. However, when trying to execute the command on the server via console (ssh), I am faced w ...

Ways to send JWT in the request header to an Angular6 application

Managing users and sessions through an external application such as a Web Application Firewall (WAF) The external app sends a JWT with user information in the request header to the Angular6 app The Angular6 app needs to extract the information from the req ...

Conceal the sidebar once user is logged in

I need a dynamic webpage; upon loading the login page, the sidebar should be hidden and the login page should occupy the full width of the page. Once the user successfully logs in, the sidebar along with all components should be displayed. I've attemp ...

Embedding Globalize.js into an Angular component

Hey there! I'm currently working on building an Angular 4 application that needs to support L10n. I've decided to incorporate globalize into my project. Below is a snippet of my App component: import { Component, OnInit } from '@angular/c ...

By clicking, dynamically add unique classes along with incrementing numerical values

I have a span tag and a button tag <span class="myspan">1</span> <button id="add">Add +1</button> var arr=["myspan1","myspan2","myspan3","myspan4"} In order to add more span tags with new classes from the array and increase the v ...

What are some ways I can optimize my Bootstrap CSS code to enhance responsiveness across different page widths?

After creating this fiddle, I noticed that my webpage layout with two texts stacked on top of each other looks great on wide screens. However, when the screen size is reduced or viewed on a mobile device, the layout gets all messed up. Take a look at this ...

Seeking assistance with setting up BxSlider installation

I'm struggling with adding bxslider to my HTML template. No matter what I try, the slider doesn't work as expected. When I preview my website, all three images are displayed together in a single column. Can someone please guide me on how to fix t ...

Best practice for setting up components in Angular 2 using HTML

I have developed a component that relies on external parameters to determine its properties: import { Component, Input } from '@angular/core'; import { NavController } from 'ionic-angular'; /* Info card for displaying informatio ...

How to Retrieve the Absolute Index of the Parent Column Using jQuery Datatables

I recently developed a custom column filtering plugin for datatables, but I've encountered a minor issue. Within each column footer, I have added text inputs, and now I am trying to capture their indexes on keyup event to use them for filtering. In ...