HTML elements consist of a tag name and a set of name/value pairs known as attributes. The <a> element that defines a hyperlink, for example, uses the value of its href attribute as the destination of the link. The attribute values of HTML elements are available as properties of the HTMLElement objects that represent those elements. The DOM also defines other APIs for getting and setting the values of XML attributes and nonstandard HTML attributes. The subsections that follow have details.

HTML Attributes As Element Properties

The HTMLElement objects that represent the elements of an HTML document define read/write properties that mirror the HTML attributes of the elements. HTMLElement defines properties for the universal HTML attributes such as id, title, lang, and dir, and event handler properties like onclick. Element-specific subtypes define attributes specific to those elements. To query the URL of an image, for example, you can use the src property of the HTMLElement that represents the <img> element:

var image = document.getElementById("myimage"); 
var imgurl = image.src;   // The src attribute is the URL of the image === "myimage"    // Since we looked up the image by id

Similarly, you might set the form-submission attributes of a <form> element with code like this:

var f = document.forms[0];                      // First <form> in the document
f.action = ""; // Set URL to submit it to.
f.method = "POST";                              // HTTP request type

HTML attributes are not case ...

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.