C
C
crayes232015-10-16 22:54:00
JavaScript
crayes23, 2015-10-16 22:54:00

How to pass variable with jquery element class?

For example, I want to change class 2 color to green
<div id="1" class="2">yo</div>
I find the class by id and put it in the "my" variable, I want the color to change on click

var my = $('#1').attr('class');
$(my).click(function() {
  $(my).css( "color", "green" )
});

But nothing happens. How to solve the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2015-10-16
@crayes23

The selector for classes starts with a dot:
$('.' + my)

I
iCat, 2015-10-16
@iCat

Имена классов начинаются с точки:

var my = '.' + $('#1').attr('class');
$(my).click(function() {
  $(my).css( "color", "green" )
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question