Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a new class to an element dynamically?

Tags:

css

addclass

Is there any way to add a new CSS class to an element on :hover?

Like for jQuery, but translated into CSS:

$(element).addClass('someclass'); 
like image 566
itsme Avatar asked Oct 14 '12 22:10

itsme


People also ask

How do I add a class to a dynamic element?

To do that, first we create a class and assign it to HTML elements on which we want to apply CSS property. We can use className and classList property in JavaScript. Approach: The className property used to add a class in JavaScript.

How can you add new elements dynamically?

New elements can be dynamically created in JavaScript with the help of createElement() method. The attributes of the created element can be set using the setAttribute() method.


1 Answers

Short answer no :)

But you could just use the same CSS for the hover like so:

a:hover, .hoverclass {     background:red; } 

Maybe if you explain why you need the class added, there may be a better solution?

like image 76
Rene Koch Avatar answered Nov 04 '22 03:11

Rene Koch