HTML DOM Cite Object: Accessing Citation Elements

The HTML DOM cite object provides a way to interact with <cite> elements in your HTML documents using JavaScript. The <cite> tag is used to define the title of a creative work (e.g., a book, song, film, research paper), and the cite object in the DOM allows you to access and modify these citation elements dynamically. This article provides a comprehensive guide to accessing and manipulating <cite> elements using the HTML DOM.

What is the HTML <cite> Element?

The HTML <cite> element is used to mark up the title of a creative work. This could be a book, article, song, film, or any other type of work that can be cited. The <cite> element is typically used in conjunction with blockquotes or other content that quotes or references external sources.

<p>According to <cite>The Hitchhiker's Guide to the Galaxy</cite>, the answer to the ultimate question of life, the universe, and everything is 42.</p>

The HTML DOM cite Object

The HTML DOM cite object represents an HTML <cite> element. It provides properties and methods that allow you to access and manipulate the element. By using JavaScript, you can change the content, style, or attributes of <cite> elements, making your web pages more interactive and dynamic.

Accessing <cite> Elements

To access a <cite> element, you can use JavaScript methods like getElementById, getElementsByTagName, querySelector, or querySelectorAll. Here are some examples:

Using getElementById

If your <cite> element has an id attribute, you can access it directly using getElementById.

<cite id="myCitation">The Lord of the Rings</cite>

<script>


  const citeElement_1 = document.getElementById("myCitation");
  console.log(citeElement_1.textContent); // Output: The Lord of the Rings


</script>

Using getElementsByTagName

You can access all <cite> elements using getElementsByTagName. This returns an HTMLCollection of all <cite> elements in the document.

<cite>Pride and Prejudice</cite>
<cite>1984</cite>

<script>


  const citeElements_2 = document.getElementsByTagName("cite");
  for (let i = 0; i < citeElements_2.length; i++) {
    console.log(citeElements_2[i].textContent); // Output: Pride and Prejudice, 1984
  }


</script>

Using querySelector

The querySelector method returns the first element that matches a specified CSS selector.

<cite class="book-title">To Kill a Mockingbird</cite>

<script>


    const citeElement_3 = document.querySelector("cite.book-title");
    console.log(citeElement_3.textContent); // Output: To Kill a Mockingbird


</script>

Using querySelectorAll

The querySelectorAll method returns all elements that match a specified CSS selector. This returns a NodeList.

<cite class="movie-title">The Godfather</cite>
<cite class="movie-title">Pulp Fiction</cite>

<script>


  const citeElements_4 = document.querySelectorAll("cite.movie-title");
  citeElements_4.forEach(cite => {
    console.log(cite.textContent); // Output: The Godfather, Pulp Fiction
  });


</script>

Important Properties and Methods

The HTML DOM cite object inherits properties and methods from its parent HTMLElement and Node objects. Here are some of the most commonly used properties:

Property/Method Type Description
`textContent` String Gets or sets the text content of the cite element.
`innerHTML` String Gets or sets the HTML content of the cite element.
`id` String Gets or sets the ID of the cite element.
`className` String Gets or sets the CSS class name(s) of the cite element.
`style` Object An object representing the inline style of the cite element.
`getAttribute(attributeName)` Function Returns the value of the specified attribute on the cite element.
`setAttribute(attributeName, value)` Function Sets the value of the specified attribute on the cite element.
`removeAttribute(attributeName)` Function Removes the specified attribute from the cite element.

Examples of Manipulating <cite> Elements

Let’s explore some examples of how you can manipulate <cite> elements using JavaScript.

Changing Text Content

You can change the text content of a <cite> element using the textContent property.

<cite id="changeCitation">Original Title</cite>
<button id="changeButton">Change Citation</button>

<script>


  const citeElement_5 = document.getElementById("changeCitation");
  const changeButton_5 = document.getElementById("changeButton");

  changeButton_5.addEventListener("click", () => {
    citeElement_5.textContent = "New Title";
  });


</script>
Original Title

Adding a CSS Class

You can add a CSS class to a <cite> element using the className property.

<cite id="classCitation">Some Title</cite>
<button id="classButton">Add Class</button>

<style>
  .highlight {
    color: blue;
    font-weight: bold;
  }
</style>

<script>


  const citeElement_6 = document.getElementById("classCitation");
  const classButton_6 = document.getElementById("classButton");

  classButton_6.addEventListener("click", () => {
    citeElement_6.className = "highlight";
  });


</script>
Some Title

Setting and Getting Attributes

You can set and get attributes using setAttribute and getAttribute methods.

<cite id="attrCitation" data-info="Initial">An Article Title</cite>
<button id="attrButton">Change Attribute</button>

<script>


  const citeElement_7 = document.getElementById("attrCitation");
  const attrButton_7 = document.getElementById("attrButton");

  attrButton_7.addEventListener("click", () => {
    citeElement_7.setAttribute("data-info", "Updated");
    console.log(citeElement_7.getAttribute("data-info")); // Output: Updated
  });


</script>
An Article Title

Changing Inline Styles

You can modify inline styles using the style property.

<cite id="styleCitation">A Song Title</cite>
<button id="styleButton">Change Style</button>

<script>


  const citeElement_8 = document.getElementById("styleCitation");
  const styleButton_8 = document.getElementById("styleButton");

  styleButton_8.addEventListener("click", () => {
    citeElement_8.style.color = "green";
    citeElement_8.style.fontStyle = "italic";
  });


</script>
A Song Title

Real-World Applications

  • Dynamic Citations: Updating citations on a page in response to user interaction or data changes.
  • Interactive Bibliographies: Creating interactive lists of references that can be expanded or filtered.
  • Content Management: Building applications where users can manage and edit citation information dynamically.
  • Accessibility: Providing better context and structure to citations, improving accessibility for users with disabilities.

Browser Support

The HTML DOM cite object is supported by all modern web browsers, ensuring compatibility across different platforms.

Conclusion

The HTML DOM cite object provides essential capabilities for accessing and manipulating <cite> elements in your web pages. By using the methods and properties described in this article, you can create more interactive, dynamic, and user-friendly web applications. Whether you are highlighting titles, updating metadata, or integrating with other dynamic components, the ability to handle citation elements with JavaScript enhances your development capabilities significantly.