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 trial

JavaScript

why we don't use always document.getElementById('id'); instead of use another complicated things?

I know its a weird question. but i would like to know the answer. thank you very much.

to be more specific:

use document.getElementById('id'); obviously when we want to target a single html element.

when we want to store multiple element can we use only one?

i mean we just have to put id (in single elements) to everything.

thank you very much in advance for answer my question I'm sure that you will help me with this.

can you explain further what you mean by complicated things? l am currently reading a book on js and think l might be able to help.

hi kelvin, I meant to say with complicated things.. traversing and all that stuff . I'm reading a book as well and trying to figure it out everything. with the haunguyen answer is more clear now. thanks.

which book you reading?

JavaScript and JQuery: Interactive Front-End Web Development By jon ducket

no way you not! l am reading the same book lol. amazing stuff he talks about man. l only wish he writes a PHP book aswell.

Yeah mate it's really good !!! I just start to read it .. And I can understand everything now !! Ok not everything haha but kind of !!

2 Answers

You can have 2 (or more) < p > tags (for example),

each p tag can have its own id

< p id = "eee" > < p id = "sdf"> </ P > < / P >

so the id allows you to differentiate the 2 p tags and access them separately.

you can also use get element by tag name, if you choose get by tag name (p), you access all the p tags.

You don't have to put an ID on every tag, just the ones that you want to conveniently access. That is up to you to decide during your design process.

good point and make sense because when we want to select multiple elements of course we don't want to put id to everything. hehe thanks.

I'm not sure what you're asking.

<div id="idValue"></div>
// this returns the single element with an id of idValue
var myElement = document.getElementById('idValue');

// something I learned recently, if an element has an id, you can
// type it's value followed by whatever you want to do

// Note: this works in my local development environment, but not on all websites with dev console
// I'm not sure what limitation decides when this works or not
idValue.classList.add(["fish", "bacon", "milk"]);

idValue.classList.remove("milk");
<div id="idValue" class="fish bacon"></div>

What kind of markdowns do you use to wrap around codes?

check out the Markdown Cheatsheet link at the bottom.

Oh thanks, I have been using the 3 ticks, but I did not read it careful enough to have included the "language" designator.

thanks you robert as well.