JavaScript JavaScript and the DOM Getting a Handle on the DOM Using CSS Queries to Select Page Elements

Stephon Ripton
Stephon Ripton
1,890 Points

Why can't we do this? document.querySelector('MyTitle');

instead we have to do it like this: ('[title=MyTitle]')?

1 Answer

Nicholas Grenwalt
Nicholas Grenwalt
46,624 Points

If you just inserted 'MyTitle' into querySelector it would be looking for a regular dom element with the name of 'MyTitle' specifically. When you use the bracket syntax you are searching for attributes that match a particular value on an attribute. In this case, they are looking for the first occurrence of an element where it's title attribute is set to MyTitle. It has nothing to do with what the actual text happens to say inside the element, but rather it is just for targeting purposes for styling, manipulation, etc..