Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Andreas Kratzel
2,858 Points.hasClass() as well for ids etc.?
Hi there,
I was wondering whether it is possible to use the .hasClass(); method for CSS-ids, names etc. as well?
For example:
<div id="id-name">
<p>insert text here</p>
</div>
And then try to add changes via jQuery like this:
if($("p").parent().hasClass("id-name")) {
// insert condition here
}:
If not - what would be an alternative for that then?
Thanks for your help!
1 Answer

Ben Schroeder
22,818 PointsI don't think it works, but even if it does, it'd be a bit of a roundabout way of selecting an element based on its ID. You could use .attr().
Because each ID on a page should only belong to one element, using normal CSS selectors would work. What specifically would you like to do with the paragraph?
$("#id-name p")
Andreas Kratzel
2,858 PointsAndreas Kratzel
2,858 PointsActually, that was just a fully random example. This question just popped into my mind while watching this video, but I agree with your comment and solution - this makes more sense! :-)
Thanks!