js_ex_dom
JavaScript and HTML Document Object Model (DOM) Examples
Section titled “JavaScript and HTML Document Object Model (DOM) Examples”This section lists examples of how JavaScript can be used to interact with and manipulate HTML elements through the DOM.
Anchor (<a>) Element
Section titled “Anchor (<a>) Element”Common tasks with link elements:
- Retrieve the
href(URL) of a link. - Get or set the
hreflangattribute (language of the linked resource). - Access the
idof a link. - Determine the
relattribute (relationship to the current document). - Find or change the
targetattribute (where to open the link). - Inspect the
typeattribute (MIME type of the linked resource).
These are typically done by selecting the element (e.g., using document.getElementById or document.querySelector) and then accessing its properties (e.g., linkElement.href).
Area (<area>) Element (within Image Maps)
Section titled “Area (<area>) Element (within Image Maps)”Interacting with clickable regions in an image map:
- Get the
alttext for an area. - Retrieve the
coords(coordinates) defining the area. - Find the
shapeof the area (e.g., ‘rect’, ‘circle’). - Access the
href(URL) of an area. - Parse components of the
href:protocol,hostname,port,pathname,search(query string),hash. - Get or set the
targetattribute for the area’s link.
Base (<base>) Element
Section titled “Base (<base>) Element”Managing base URLs for relative links:
- Retrieve the
hrefattribute to find the base URL for all relative URLs on a page. - Get the
targetattribute to find the default target for all links on a page.
IFrame (<iframe>) Element
Section titled “IFrame (<iframe>) Element”Manipulating embedded inline frames:
- Access the
contentDocumentorcontentWindowto interact with the iframe’s content (subject to same-origin policy). - Change the
style.backgroundColorof the iframe element itself (not its content directly without access). - Get or set the
heightandwidthattributes of the iframe. - Retrieve the
nameattribute of an iframe. - Get or change the
src(source URL) attribute of an iframe, effectively loading new content.
Modern practice might also involve postMessage for cross-origin communication with iframes.
Image (<img>) Element
Section titled “Image (<img>) Element”Working with images:
- Get or set the
alttext for an image. - Retrieve the
heightandwidthof an image (actual or specified). - Implement dynamic image loading, e.g., display a high-resolution version on click.
- Get or change the
src(source URL) of an image. - Dynamically change an image based on user interaction (e.g., toggle a lightbulb image).
- Access the
usemapattribute if the image is part of an image map.
Table (<table>, <tr>, <td>, etc.) Elements
Section titled “Table (<table>, <tr>, <td>, etc.) Elements”Manipulating HTML tables:
- Change table appearance, e.g.,
table.style.borderWidthor CSS classes forborder-spacingandpadding. - Get or set the
innerHTMLortextContentof a table cell (<td>or<th>). - Create and append a
<caption>element to a table usingtable.createCaption(). - Add or delete rows using
table.insertRow()andtable.deleteRow(). - Add or delete cells within a row using
row.insertCell()androw.deleteCell(). - Modify the content of existing table cells.
Modern applications might use JavaScript frameworks or libraries for complex table manipulations and data binding, but understanding core DOM methods is essential.