Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Web and personalized recommendations.Start Your Free Trial Now
- View Offline
- Creating new HTML5 documents
- Using code hinting for CSS3, HTML5, DOM elements, and jQuery
- Configuring jQuery widgets with the Widget Browser
- Managing media queries
- Previewing web pages on multiple devices with the Multiscreen Preview panel
- Configuring mobile application frameworks
- Building and emulating mobile applications
Skill Level Intermediate
So, var targetElement, and I need to go ahead and set this value. Now, I want to target specific elements on the page, and I want to target any element that has an ID of pageID. So to do that, I am going to type in 'document' and my code hinting comes up, and now I want to do getElementById, so I can just type in 'get' and I am going to let code hinting handle the rest of this for me. So if I hit Return or Enter, it will finish that for me, and now I need to go ahead and give it an ID. Now, what if I don't have all the IDs in my parent document memorized? Well, if I type in a quotation mark, I now get a list of all of the IDs in my parent file.
This is really cool! So I can scroll down through the list and I can find pageID, because remember, that's the one we're looking for. If I hit Return or Enter, now all I have to do is close my parentheses and type in a semicolon. All right! Now, we need to go ahead and change its color, so I'm going to go down to the next line and I'm going to type in 'targetElement.style.color = "red"'. All right, cool! So code hinting helped with that entire process, but the really cool thing there was going out to my parent page and grabbing the pageID from the code.
So I'm going to go ahead and save that, and then I want to preview this in a browser, and when I do that, I can see the element that has the pageID is now red. So let's go back in our code, and let's check out some of the other code hinting improvements that have been made. So the next thing I want to do is I want to get rid of this getElementById, and let's try getting elements by their tag name. So I'm going to type in 'document.getElementsByTagName', and now when I type in a single quote, I get an entire list of HTML elements.
This is the same list that you get when you're hand-coding in Dreamweaver in just a normal HTML document, so they've added that as well. And now I just want do maybe all heading 1s. So I'm going to do h1 and hit Return and then close my parentheses. Now, obviously we're going to get more than one of those, so I need to create a for loop that's going to loop through them and apply the color to each one of these. So just on top of my target element, I'm just going to go ahead and create a loop. So I am going to do for(var i=0; and I am going to make i<targetElement.length, and we'll just go ahead and add one to i.
Now, I am going to ahead and place the style assignment inside my for loop. And of course I need to reference each item in the array that's returned, so I'm just going to go ahead and in bracket notation pass i in. Okay. So now let me go ahead and save this and preview this in the browser as well. And now if I scroll down, I can see that all the heading 1s on the page are now red. Perfect! Now, in addition to giving you a list of tags and giving you a list of IDs, it will also introspect all of your class names from your file as well.
So I now want to go ahead and get rid of my getElementsByTagName, and I'm going to change that to getElementsByClassName. Now, if you're wondering, it does support both the document.getElementsByClassName as well as the element.getElementsByClassName, so it will work both ways. Again, if I type in a single quotation mark, now I get all of the classes in my document. I can see that I can just scroll through all of them. And I'm just going to do accent, the first one that comes up. And now I'll close my parentheses, and we shouldn't need to change anything down here.
So I'm going to go ahead and save that, preview that in the browser, and now you can see that only where the accent class has been applied, which has been applied to this header and this header, is red. Cool! So this code hinting works for getElementById, getElementsByName, getElementsByTagName, and getElementsByClassName. What this really means more than anything else is that when you're scripting, you're going to be able to speed up the process of targeting elements on your pages, even when you're working in remote files. Dreamweaver's ability to introspect those pages and then return elements and their IDs and then classes will really just end up being one more of those little things that Dreamweaver does to help speed up your development process.