JavaScript Only getting parent tag name, not child on click event using prop(“tagName”). Why?

I want to get the tags name when I click on them, I have applied a common class on each tag. Now the proplem is when ever I click on p tag or h1 it always gives the parent name.

$(function(){
  $('.r').click(function(event) {
    // $('.r').each(function() {
    event.stopPropagation();
    var $detect = $('.r').prop('tagName');
    // alert($detect);
    console.log($detect);
  });
  // })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="r">
	<h1 class="r">Title H1</h1>
	<p class="r">Para</p>
</div>
Answer:1

$('.r') will always give you the first-element.

So

Either Use $(this):-

$(function(){
  $('.r').click(function(event) {
    event.stopPropagation();
    var $detect = $(this).prop('tagName');//$(this) will give you current clicked object
    console.log($detect);
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="r">
  <h1 class="r">Title H1</h1>
  <p class="r">Para</p>
</div>
Answer:2

$(this) instead of $('.r')-

$(function() {
  $('.r').click(function(event) {

    // $('.r').each(function() {
    event.stopPropagation();
    var $detect = $(this).prop('tagName');
    // alert($detect);
    console.log($detect);
  });
  // })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="r">
  <h1 class="r">Title H1</h1>
  <p class="r">Para</p>
</div>
Answer:3

I am trying to figure out how to use propeller.js to restrict the rotation angle of an image. As of right now, if I spin my image twice, my output would read 720. I would like to make it so the ...

I am trying to figure out how to use propeller.js to restrict the rotation angle of an image. As of right now, if I spin my image twice, my output would read 720. I would like to make it so the ...

i tried with this RegEX no solution ^([a-zA-Z]{1})([0-9]{3})$ Test Case: Valid 123d f311 12d3 99A9

i tried with this RegEX no solution ^([a-zA-Z]{1})([0-9]{3})$ Test Case: Valid 123d f311 12d3 99A9

  1. regex three letter word
  2. regex three letter month
  3. regex three letters

I have an existing Django project and am adding a new app which consists of an API (using the rest-framework) and an angular app. This app contains one Django template, which simply serves as a way ...

I have an existing Django project and am adding a new app which consists of an API (using the rest-framework) and an angular app. This app contains one Django template, which simply serves as a way ...

I'm just a beginner I want to create button for remove clone but I can't. This is my code but it isn't work. Please help me to know where did I wrong. PS. Sorry for my poor English HTML <div ...

I'm just a beginner I want to create button for remove clone but I can't. This is my code but it isn't work. Please help me to know where did I wrong. PS. Sorry for my poor English HTML <div ...