New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Changing DOM elements

From: JavaScript Essential Training

Video: Changing DOM elements

Using JavaScript to read information about the nodes on our page is all very well, but we really want to be able to change it. Step one for doing this is always going to be get the element. You need that variable to be a handle on the element. Sure, we know that we've got elements, and we also have attributes and text nodes, but it's the element that comes first. Attribute nodes and text nodes belong to elements. If you're writing HTML, you don't write an attribute by itself. It's the attribute of an element.

Changing DOM elements

Using JavaScript to read information about the nodes on our page is all very well, but we really want to be able to change it. Step one for doing this is always going to be get the element. You need that variable to be a handle on the element. Sure, we know that we've got elements, and we also have attributes and text nodes, but it's the element that comes first. Attribute nodes and text nodes belong to elements. If you're writing HTML, you don't write an attribute by itself. It's the attribute of an element.

So that's what comes first. Step two is okay, then we can change it. The question is, what do you want to change about it? If you were manually editing this code in a text editor, what would you want to do? Do you want to pick an attribute and change it or add a new attribute? Do you want to change a link inside a heading? Do you want to add a whole bunch of new elements inside a div or inside a list? Well, we can do all of that, but let's talk about changing existing elements, rather than creating new ones.

We'll get to that in a minute. One of the simplest changes we could make is to change an attribute of an element. Now once we've got that element, JavaScript actually has two methods we can use to directly work with its attributes: there's getAttribute and setAttribute. getAttribute is very simple. We just need to say what attribute. You pass this as a string in double quotes. It's the name of the attribute: align, title, class, source. setAttribute very similar, but we need a bit more information.

We pass in a value. So we call the setAttribute method. We pass it the name in quotes. We pass it the value in quotes, always as a string. If the attribute doesn't currently exist, it will be created. Let's take a look at this. So I have a simple HTML page that I have here. There's nothing remarkable about it. It's just to have something to work with. You could work with a test page yourself. I have that open in my editor here, and down at the bottom, I have a link to my script.js file, which is in the same folder, which is actually over here.

It has a few lines, but they're all commented out, just so you don't have to watch me type code all the time. So what I'm going to do is I want to grab hold of a section of this page and change it. The thing I'm going to grab is actually this div here on Line 22. It's got an ID of mainContent. It doesn't currently have any attributes set, but we can change that. So over in my JavaScript I am just going to uncomment Line 1 here. It's just a document.getElementByID of mainContent storing it in a variable called mainContent.

Then I can use that variable, and just call setAttribute. The attribute is align, and the value is right. So I'll just save that JavaScript file, flick over into Firefox, and this is the area that I should be affecting. That's what that div is containing. So if I refresh this page, yes indeed, we've got align right now. So it's a very easy task to just change the attributes of an element that you have a handle to. Well, what if we want to get a little bit deeper than that? What if I want to change the contents of an element? Say I've got an h1 that I want to change or a paragraph that I want to change.

Well, you have a couple of options here. The simplest option is a method called innerHTML, and that's inner in lowercase, and HTML in uppercase. You can use that on any element you have. It allows you to either read or change the entire contents of that element. So let's say here I've got an anchor tag with an ID of mainTitle. Right now, it contains the words "Welcome to Explore California!" I am going to jump over in my JavaScript, and uncomment a little line there where I am creating mainTitle and just using document.getElementByID of mainTitle.

I'm now going to uncomment just a console.log message and ask for mainTitle's innerHTML property, save that, jump back over, and I'll refresh this page. You need to have Firebug open to see that. The console.log message is writing out Welcome to Explore California! So in this case, innerHTML of this anchor tag is giving us the text inside it. When you have a very simple tag, innerHTML can be a good way to work with it.

A simple h1 is fine, as is an a tag, as is even there's a paragraph. Grabbing, for example, a div might give you a little more. Let's say we get a div that contains quite a few things, like this div ID of sidebar. If I do exactly the same kind of thing here, let's just change that and call it a var sidebar, and log out its innerHTML, jump back over to Firefox, and refresh the page, well, you see what we're getting here is the entire contents of that div and all its divs inside it and all its h2s.

This certainly wouldn't be anywhere near as easy to start to manipulate as it would be if I just wanted to change the title or the words of a paragraph, or an h1. So if I want a little more finesse, I'm not going to use innerHTML. If it's an element that contains other elements, I might have a bit more thinking to do. The best way to explain how we actually start to drill down into the interior of the page probably comes from learning how to create elements. While you could use innerHTML and manually type all the HTML yourself in your JavaScript, that's not the best way to do it, as we'll see in a moment.

Show transcript

This video is part of

Image for JavaScript Essential Training
JavaScript Essential Training

56 video lessons · 99532 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 3m 28s
    1. Welcome
      1m 1s
    2. What you should know
      1m 44s
    3. Using the exercise files
      43s
  2. 15m 41s
    1. Introduction to JavaScript
      8m 6s
    2. Creating your first JavaScript
      2m 13s
    3. Getting to know the tools and applications
      5m 22s
  3. 56m 8s
    1. Understanding the structure of JavaScript code
      7m 9s
    2. Where to write your JavaScript
      3m 56s
    3. Creating variables
      6m 21s
    4. Working with conditional code
      5m 44s
    5. Working with operators
      13m 28s
    6. Sending messages to the console
      2m 59s
    7. Working with loops
      8m 1s
    8. Creating functions
      8m 30s
  4. 36m 13s
    1. Working with arrays
      7m 57s
    2. Working with numbers
      6m 13s
    3. Working with strings
      8m 27s
    4. Working with dates
      5m 38s
    5. Working with objects
      7m 58s
  5. 9m 6s
    1. What is the DOM?
      5m 49s
    2. Working with nodes and elements
      3m 17s
  6. 25m 17s
    1. Accessing DOM elements
      11m 3s
    2. Changing DOM elements
      5m 42s
    3. Creating DOM elements
      8m 32s
  7. 24m 45s
    1. Introduction to JavaScript event handling
      8m 16s
    2. Working with onClick and onLoad events
      7m 36s
    3. Working with onBlur and onFocus events
      2m 36s
    4. Working with timers
      6m 17s
  8. 21m 41s
    1. Common JavaScript errors
      7m 14s
    2. Using Firebug
      4m 7s
    3. Going through a debugging session
      10m 20s
  9. 10m 13s
    1. Accessing form elements
      4m 20s
    2. Preventing a form from being submitted
      2m 36s
    3. Hiding and showing form sections
      3m 17s
  10. 9m 49s
    1. CSS and JavaScript
      3m 46s
    2. Removing and applying CSS classes
      2m 16s
    3. Changing inline styles
      3m 47s
  11. 19m 44s
    1. Understanding JavaScript style
      7m 39s
    2. Minifying your code
      4m 28s
    3. Using JavaScript code checkers
      7m 37s
  12. 22m 24s
    1. Introduction to JavaScript libraries
      3m 17s
    2. Linking to multiple JavaScript files
      2m 11s
    3. Introduction to jQuery
      12m 7s
    4. Using a content distribution network to deliver JavaScript files
      4m 49s
  13. 17m 35s
    1. JavaScript in HTML5
      9m 37s
    2. Using Modernizr
      3m 2s
    3. Using Strict Mode
      4m 56s
  14. 33m 3s
    1. Knowing the JavaScript to avoid
      6m 35s
    2. Introduction to regular expressions
      6m 56s
    3. Working with AJAX
      10m 44s
    4. Working with objects and prototypes
      8m 48s
  15. 21m 10s
    1. Example: Countdown
      8m 3s
    2. Example: Resize
      5m 47s
    3. Example: Accordion
      7m 20s
  16. 4m 58s
    1. Where to go from here
      4m 0s
    2. Goodbye
      58s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.