For each anchor, look “upwards” in the DOM tree to find the closest heading (an <h1> or <h2>) that appears before it in the node chain. (You could interpret “nearest” in a few ways; here we recursively check previous siblings and then repeat the search on the parent element.)
Create an object containing the URL (from the anchor’s href), the anchor’s text, and the heading text (if found).
You can run this code in your browser’s console on the site in question:
javascript
// A helper function to recursively search for a preceding h1 or h2 element
function getNearestHeading(el) {
if (!el) return null;
// Check previous siblings of the current element
let sibling = el.previousElementSibling;
while (sibling) {
if (sibling.matches('h1, h2')) {
return sibling;
}
sibling = sibling.previousElementSibling;
}
// If not found on this level, move up to the parent and try again.
return getNearestHeading(el.parentElement);
}
// Get all <a> tags in the document
const anchorElements = document.querySelectorAll('a');
const results = Array.from(anchorElements).map(a => {
// Get the nearest heading for the given anchor
const nearestHeading = getNearestHeading(a);
return {
url: a.href,
linkText: a.textContent.trim(),
headingText: nearestHeading ? nearestHeading.textContent.trim() : null
};
});
console.log(results);
How It Works
getNearestHeading(el) Function:
For a given element, it first looks among its previous siblings to see if any is a heading (h1 or h2).
If no heading is found among the siblings, it “climbs” one level up (to the parent element) and repeats the search.
If ultimately no heading is found, it returns null.
Mapping Over <a> Elements:
With document.querySelectorAll('a') we get every anchor tag.
For each anchor, we extract:
url: the href attribute,
linkText: the trimmed text content of the anchor,
headingText: the text content of the nearest heading found (or null if none).
Output:
The array of objects is printed to the console.
Feel free to adjust the definition of “nearest” to better fit your document’s structure if needed.
User
Try to use the builtin dom function "nearest"
Assistant
The DOM provides a built‐in method called Element.closest() which “climbs” the DOM tree starting from the current element and returns the first ancestor (which can include the element itself) that matches the given selector. In many cases the nearest heading is an ancestor of the link, so you can use this method to find a heading element (either an <h1> or <h2>) above the <a> tag.
Below is an example script that:
Collects all <a> tags on the page.
For each anchor, uses closest('h1, h2') to find the nearest ancestor heading.
Constructs an array of objects where each object contains:
url: the link’s href
linkText: the text content inside the <a> tag
headingText: the text content of the found <h1> or <h2>, or null if none is found
Note:
The built-in closest() method only traverses upward through the DOM (i.e. the current element’s ancestors). If in your page the appropriate heading might actually be a sibling or elsewhere in the DOM, you would need to add additional logic.
Here’s the code:
javascript
// Get all <a> elements on the document
const anchorElements = document.querySelectorAll('a');
const results = Array.from(anchorElements).map(a => {
// Use the built-in closest() method to find the nearest heading ancestor (h1 or h2)
const headingEl = a.closest('h1, h2');
return {
url: a.href,
linkText: a.textContent.trim(),
headingText: headingEl ? headingEl.textContent.trim() : null
};
});
console.log(results);
How This Works
a.closest('h1, h2'):
This call starts at the anchor element and traverses up through its ancestors. If it finds an ancestor that is an <h1> or <h2>, it stops and returns that element. If no matching ancestor is found, it returns null.
Generating the Results Array:
For each anchor, we create an object with the URL (href), the text inside the <a> tag, and the text of the heading (if one was found).
This script can be pasted into your browser’s JavaScript console on the target website to see the array of URL objects along with their associated headings.