Attributes
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:
varimage=document.getElementById("myimage");varimgurl=image.src;// The src attribute is the URL of the imageimage.id==="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:
varf=document.forms[0];// First <form> in the documentf.action="http://www.example.com/submit.php";// Set URL to submit it to.f.method="POST";// HTTP request type
HTML attributes are not case ...
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