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

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

any reason why these wont work? ('li:nth-child(even)') this wont work if there is space b/w semi colon and nth-child

secondly can we not use querySelectorAll on getElementsByTagName('li') ??

I think if you put a space after the li selector you're selecting the nth-child of descendants.

3 Answers

Steven Parker
Steven Parker
221,328 Points

Ben's right about not using a space there. And you certainly can use querySelectorAll on getElementsByTagName('li'), but that might not be the solution you need.

For a more complete answer, please show the whole code, and provide a link to the course page so we can see what you're working on.

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

hi this is the code i was referring to!! https://w.trhou.se/vu1g2aeec5

it doesnt work when i use querSelectorAll('li') plus i thought space is invalid ...between semi colon and nth-child

Steven Parker
Steven Parker
221,328 Points

Yes, space is invalid there. But it's a colon :point_right: : (this is a semicolon :point_right: ; ).

And the code in your snapshot seems to be working. Every other line has a grey background. Can you describe what's happening (or not happening) that seems wrong?

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

ah sorry yeh i meant colon:
ok this code is the one where theres space b/w colon and nth child and it doesnt work for even lists coloured as grey, but when i omit space it works fine! its not big issue i can remember to not leave space b/w them but wondering is it the general practice or is there something wrong with it? coz i taught myself that space doesnt matter in JS. where as the other issue worked just fine i tried replacing "getElementsByTagName('li')" with querySelector('li') and it worked ,after i sent u this link for the workspace. https://w.trhou.se/s2gj44q4wd

Steven Parker
Steven Parker
221,328 Points

This is CSS, not JS. And spaces do matter, sometimes. One place they matter for sure is in selectors. A space creates a "descendant selector". And a colon must come directly in front of a pseudo-class, just like a period must come directly in front of a class.

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

oh yeh that indeed is css and not js ....thanks steven for helping clear my mind !