Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 class="hello">
   <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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div class="hello">

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

>Solution :

Use .attr()

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

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading