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.

JavaScript

Ben Os
Ben Os
20,008 Points

How to connect id and class in document.querySelector ?

Say I want to target an element that has BOTH id and class with querySelector, to later manipulate it,

How can I target the element with this combination?

1 Answer

Owa Aquino
Owa Aquino
19,277 Points

Hi Ben,

I think there's no way doing this in the query selector and even in your css style sheet.

And if ever it's possible this would be a bad practice.

The purpose of using ID selector is selecting a single element that is unique, while using Class selector is selecting multiple elements with the same qualities. By selecting both ID and Class at the same time it would be difficult for the browser to select a specific elements, it will always return multiple elements (if somehow it's posible to select both Class and ID in the query selectors).

This is only my opinion though, Hope this helps.

Cheers!

Owa