ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

HTML Tag Attribute Value Extraction

HTML

Goal -- WPM

Ready
Exercise Algorithm Area
1function getFirstAttributeValue(htmlString, tagName, attributeName) {
2if (!htmlString || typeof htmlString !== 'string') {
3console.error('Invalid HTML content.');
4return null;
5}
6if (!tagName || typeof tagName !== 'string' || tagName.length === 0) {
7console.error('Invalid tag name.');
8return null;
9}
10if (!attributeName || typeof attributeName !== 'string' || attributeName.length === 0) {
11console.error('Invalid attribute name.');
12return null;
13}
14
15const parser = new DOMParser();
16const doc = parser.parseFromString(htmlString, 'text/html');
17const elements = doc.getElementsByTagName(tagName);
18
19if (elements.length === 0) {
20// Tag not found
21return null;
22}
23
24const firstElement = elements[0];
25const attributeValue = firstElement.getAttribute(attributeName);
26
27if (attributeValue === null) {
28// Attribute not found on the first element
29return null;
30}
31
32return attributeValue;
33}
34
35// Example Usage:
36// const html = '<html><body><div id="main" class="container">Content</div><div data-info="test">More</div></body></html>';
37// const divId = getFirstAttributeValue(html, 'div', 'id');
38// console.log('First div ID:', divId); // Expected: "main"
39// const divClass = getFirstAttributeValue(html, 'div', 'class');
40// console.log('First div Class:', divClass); // Expected: "container"
41// const divData = getFirstAttributeValue(html, 'div', 'data-info');
42// console.log('First div Data-info:', divData); // Expected: "test"
43// const spanId = getFirstAttributeValue(html, 'span', 'id');
44// console.log('Span ID:', spanId); // Expected: null
45// const divStyle = getFirstAttributeValue(html, 'div', 'style');
46// console.log('Div Style:', divStyle); // Expected: null
Algorithm description viewbox

HTML Tag Attribute Value Extraction

Algorithm description:

This algorithm extracts the value of a specified attribute from the first HTML element that matches a given tag name. It parses the HTML, finds the first instance of the tag, and then retrieves the attribute's value. This is useful for dynamically fetching configuration data or identifiers embedded within HTML elements.

Algorithm explanation:

The `getFirstAttributeValue` function begins with robust input validation, checking for valid HTML content, tag name, and attribute name. It then parses the HTML string into a `Document` object using `DOMParser`. `doc.getElementsByTagName(tagName)` is used to find all elements of the specified tag. If no elements are found (`elements.length === 0`), it returns `null`. Otherwise, it takes the first element (`elements[0]`) and uses `firstElement.getAttribute(attributeName)` to retrieve the attribute's value. If the attribute does not exist on the element, `getAttribute` returns `null`, which is then returned by the function. The time complexity is O(N) due to the DOM traversal by `getElementsByTagName`, where N is the total number of nodes. Space complexity is O(M) for the `HTMLCollection`, where M is the number of matching tags. Edge cases include missing tags, missing attributes, and invalid inputs.

Pseudocode:

function getFirstAttributeValue(htmlString, tagName, attributeName):
  validate htmlString, tagName, attributeName
  parse htmlString into document
  find all elements with tagName
  if no elements found:
    return null
  get the first element
  get the value of attributeName from the first element
  return the attribute value (or null if not found)