Home > Net >  Select and output to console the class of the parent div using jquery
Select and output to console the class of the parent div using jquery

Time:02-26

How would I output to console the value of the parent div's class? My current code outputs the entire HTML line

i.e if I have this

<div >
   <a id="foo"></a>
</div>
$(document).on('click', 'a', function(e) {
   console.log($(this).parent("div"));
}

Why do I keep getting this in my console?

<div >

The only thing I want console to show is "hello" (without the quotations).

CodePudding user response:

Use .attr()

console.log($(this).parent("div").attr("class"));

https://api.jquery.com/attr/

  • Related