D
D
DotDash2018-05-24 01:00:06
JavaScript
DotDash, 2018-05-24 01:00:06

How to store the value of a class into a variable?

How can the content of an element's class attribute be stored into a variable if the element has multiple classes?
example
The task before me is this: when hovering over an element, save the value of the attribute into a temporary variable, change the classes, and after removing the hover, return the classes from the variable to their original place. The problem is that I do not know in advance what classes will be there.
I tried this way:
var temp1 = banner.attr('class');
banner.removeClass().addClass("red");
banner.attr(temp1);
but it didn't work (it can be seen in the example)

How would it be done correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VoidVolker, 2018-05-24
@DotDash

var c = $('#item').attr('class');
$('#item').attr('class', c);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question