JavaScript get HTML elements from a string

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

I recently had a string with some content from a WYSIWYG editor (What you see is what you get). In there, I needed to find all the href elements.
But this specific approach can work for many things.
My approach consists of using the DOMParser, one could also use a regex approach to finding all the links in a text, but I needed an HTML output back again, so for me, this worked best.
To get started, let's first define our HTML.
We will be using a variable, which you can consider came from our CMS.
const text = `<p>Some text</p><br /><a href="https://daily-dev-tips.com/">My website</a><hr /><a href="https://google.com">Another link</a>`;
As you can see, we have two links in there. Let's say we want to parse each link to add a target="_blank".
We can leverage the DOMParser to convert this string into a dom element.
let parser = new DOMParser();
const doc = parser.parseFromString(text, 'text/html');
console.log(doc);
This console.log will return the following object.

As you can see, this is a full document.
To get all the links, we can use regular queries on this doc const.
links = doc.getElementsByTagName('a');
console.log(links);
// HTMLCollection(2) [a, a]
Nice, we got our two links. We can loop over these two links and manipulate them. This will be adjusted in our doc variable.
[...links].forEach(link => {
link.setAttribute('target', '_blank');
});
Here we loop over the getElementsByTagName results, and set the target to a blank page.
Now, if we would log the current status:
console.log(doc);
We get the following result. You can see the links now have a target blank.

Let's also take some time to output the changes to see them in the HTML action.
Let's add two divs to our HTML.
<div id="original"></div>
<div id="output"></div>
First, we have our basic text variable.
const text = `<p>Some text</p><br /><a href="https://daily-dev-tips.com/">My website</a><hr /><a href="https://google.com">Another link</a>`;
Next, we will get the two div elements.
const original = document.getElementById('original');
const output = document.getElementById('output');
For the original one, we can immediately add the output as-is.
original.innerHTML = text;
And for the output one, we do our modifications as seen above.
let parser = new DOMParser();
const doc = parser.parseFromString(text, 'text/html');
links = doc.getElementsByTagName('a');
console.log(links);
[...links].forEach(link => {
link.setAttribute('target', '_blank');
});
output.innerHTML = doc.documentElement.innerHTML;
That's it. We now have two divs, one with links that open in the same tab and open in a blank tab.
Try it out using the following Codepen.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter