Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAndreas 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!