I'm creating a simple text editor in html and I have a function which allow user to insert website link into a text. Here is the code:
function setUrl() {
window.url = document.getElementById('txtFormatUrl').value;
window.sText = document.getSelection();
var urlanchor = document.createElement("A");
var urlatt = document.createAttribute("href");
urlatt.value = window.url;
urlanchor.setAttributeNode(urlatt);
window.sText = urlanchor;
}
How it works is that there will be a place to edit text and a box to enter URL. The user first highlighted the text then enter the URL, after that. The user presses the insert button which will called the setUrl() function. But when I try, the URL didn't get inserted into the text, when open the F12 console, I saw that the element don't get insert. So what's wrong with my code?