function createLink(href, text, ...classes) {
// Create the anchor:
const link = document.createElement('a');
// Add the href:
link.href = href;
// Create the inner element + the text to go inside:
const inner = document.createElement('span');
const innerText = document.createTextNode(text);
// Add any classes
inner.classList.add(...classes);
// Append the text
inner.appendChild(innerText);
// Append that inner span to the anchor
link.appendChild(inner)
// Now have a full link ready to put into the document:
return link;
}
// When you want to create & add the link:
const myLink = createLink("www.example.com", "I'm a link", "exampleClass1", "exampleClass2");
document.body.appendChild(myLink);