If I create a text in the following way:
function urlify(text) {
var urlRegex =/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%? =~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(urlRegex, function(url) {
return '<a href="' + url + '">' + url + '</a>';
});
}
document.addEventListener("DOMContentLoaded",function(){
var m = document.getElementById("m");
var t = prompt('Ingresa texto','Texto:');
var p = "";
var nombre = prompt('nombre de usuario','');
var testd = urlify(t);
if(testd.indexOf('href="') != -1) {
p = document.createElement("A")
}
else {
p = document.createElement("P")
}
var texto = document.createTextNode(nombre + ": " + testd);
p.style.color = "white";
p.appendChild(texto);
m.appendChild(p);
});
#m {width:300px:height:200px;background-color:rgba(21,21,21,0.75)}
<div id="m">
</div>
I explain:
I create the controller addEventListener
, to execute when all the elements are loaded, I define m
that is the div, I define t
that is the text that I will enter, I define p
that will be a createElement
, according to a IF
, this if
will be true
according to a funcion
that verify if it is a link of type link , if true is verified, create an A element , < strong> but a P , then I create the text with createTextNode
and in the arguments, I pass the name , a separator ": "
and the < strong> text , so it would look like:
User: message
The problem is that how can I make the name appear OTHER than the message, and also when I try to CREATE element A, it does not appear as LINK