Skip to Content
JavaScript Cookbook
book

JavaScript Cookbook

by Shelley Powers
July 2010
Intermediate to advanced
552 pages
12h 40m
English
O'Reilly Media, Inc.
Content preview from JavaScript Cookbook

Chapter 12. Creating and Removing Elements and Attributes

12.0. Introduction

The existing Document Object Models provide a plethora of methods you can use to create new web document elements. Most of the methods I use in this chapter and in the following chapters are from the DOM Levels 1 and 2 and, since most of the examples in this chapter are specific to HTML or XHTML documents, the methods and objects described inherit functionality from both Core and HTML DOM specifications.

There is one older property, innerHTML, from the nonstandard DOM Level 0, that I’ll also demonstrate, primarily because it’s so popular, and also because of new support in HTML5.

Most of the methods and associated properties are available with all of the modern browsers. I’ll make a note where a method or property isn’t supported by one or more browsers.

See Also

See the Introduction to Chapter 11, for a more in-depth look at the Document Object Model and the DOM levels.

12.1. Using innerHTML: A Quick and Easy Approach to Adding Content

Problem

You want to add a couple of paragraphs with text to a div element, and you want to do so quickly and easily.

Solution

Use the innerHTML property to overwrite an element’s existing contents, with new material:

var div = document.getElementById("target");
div.innerHTML = "<p>This is a paragraph</p><p>This is a second</p>";

Discussion

The innerHTML property has been around for a very long time, and is part of what is known as DOM Level 0—the first de facto API developed by the browser ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Object-Oriented JavaScript - Second Edition

Object-Oriented JavaScript - Second Edition

Stoyan STEFANOV
React Cookbook

React Cookbook

David Griffiths, Dawn Griffiths

Publisher Resources

ISBN: 9781449390211Errata Page