-1

How can I find out that $(this) is an li? Is there any jQuery function that will let us know what HTML tag it is?

<ul>
  <li class="data">Hello</li>
  <li class="data">World</li>
</ul>
$('.data').each(function() {
   alert($(this).?); // Find what HTML Tag is in $(this)
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
JJ D. Lordes
  • 91
  • 1
  • 7

2 Answers2

0

To determine the type of the Element you can use the tagName property:

$('.data').each(function() {
  console.log(this.tagName);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li class="data">Hello</li>
  <li class="data">World</li>
</ul>

Similarly you can use jQuery's is() method to check the given element against a selector:

$('.data').click(function() {
  if ($(this).is('span')) {
    console.log('You clicked the SPAN!');
  } else {
    console.log('You clicked something else');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span class="data">I'm a span!</span>
<div class="data">I'm a div!</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

This could help you to identify what element type is..

jQuery('.data').each(function() {
   console.log(this.nodeName);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
  <li class="data">Hello</li>
</ul>
<p class="data">para</p>
<h1 class="data">Heading 1</h1> 
Akhil Aravind
  • 5,741
  • 16
  • 35