C
C
crayes232015-10-17 13:08:17
JavaScript
crayes23, 2015-10-17 13:08:17

How to get clicked jquery object?

For example, there are two blocks with the same id

<div id="1" class="one">yo</div>
<div id="1" class="two">me</div>

I want to get the class of the clicked object on click and change its color to green
$('#1').click(function(event) {
  var myClass = $( event.target ).attr('class');
  $('.' + myClass).css( "color", "green" );
}

But nothing happens. How to solve this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Zhukov, 2015-10-17
@crayes23

The ID must necessarily begin with a Latin character and can contain Latin letters (A–Z, a–z), numbers (0–9), hyphens (-), and underscores (_).
The ID in the document code must be in a single copy .
You can use the generic on method

$(".sort a").on("click", function() {
    //...
  });

E
Everal, 2015-10-17
@Everal

$(this)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question