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
Desmond Dallas
6,985 Pointsgetelement
Can someone please explain this in the most simple terms. What it does and why you would use it. I'm completely lost even though I have read several explanations about it :(
document.getElementById
4 Answers
stjarnan
Front End Web Development Techdegree Graduate 56,488 PointsHi Desmond!
Let's say you have an html page containing nothing but two divs. One of the divs has an id of "theSpecialOne".
Now let's say that for some reason you want to target this div in your JavaScript, maybe you want to change the color? To do this you retrieve this div to your JavaScript by using:
var variableToSaveDivIn = document.getElementById('theSpecialOne');
Basically, you use that code-snippet to target and retrieve an element with an id from your html page.
Does that help you?
Jonas
stjarnan
Front End Web Development Techdegree Graduate 56,488 PointsYou're on the right way Desmond!
You will preferably use css for color etc as you say, so why would you use JavaScript? Maybe you want to add a clickevent to your element, then you use getelementbyid to target the element you want the clickevent on. An example below, let's pretend we have a div with the id of "desmond"
var specialDiv = document.getElementById('desmond');
specialDiv.addEventListener('click', function(){
alert('Desmond is awesome!');
});
If you click on the div with the id of "desmond", an alertbox will show up, if you click on another div, nothing happens. Do you understand?
You can only have one item with a specific id per html page.
I hope this helps.
Jonas
Desmond Dallas
6,985 PointsGreat thanks very much. I'm beginning to realise that one of my problems is knowing exactly what is a div, element, event etc.
When you say you can only have one item with a specific id does that mean I can only have one getepement per HTML page or just one per item?
stjarnan
Front End Web Development Techdegree Graduate 56,488 PointsYou can have as many getElementById()'s as you would like, but back in your html page you only want 1 item with a particular id as the idea is that an ID should be just that, an ID, something unique.
For example you have multiple divs on your page, each with its own unique id, you can target each one of them in your JavaScript by using document.getElementById('IdOfTheItemYouWantHere');
Jonas
Desmond Dallas
6,985 PointsFully get you now thanks.
Desmond Dallas
6,985 PointsDesmond Dallas
6,985 PointsYes, I think so now.
So say if I have several divs with different id's. Can I use one getElementById or do I have to repeat the same sequence with several getElementById's?
Another thing, I thought you would use CSS to change text/color etc. Why would I use Javascript? Wouldn't it be simpler to use CSS? Hope I'm on the right track and not completely lost???????