Mettre un lien dans une balise <label>
Bonjour,
J'ai créé un code pour transformer le texte d'un élément label en lien lorsque le champ est rempli.
Ce code fonctionne en partie mais pas sur l'événement 'DOMContentLoaded'. Je voudrais comprendre pourquoi.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| "use strict";
(function()
{
const mails = document.querySelectorAll('.mail')
, urls = document.querySelectorAll('.url')
;
function doLinkForMail(id, string)
{
let label = document.querySelector("label[for='"+id+"']")
;
if (string)
{
string = string.toLowerCase();
if (string.indexOf('string:') == -1) { string = 'mailto:'+string; }
label.innerHTML = "<a href='"+string+"'>"+label.innerHTML+"</a>";
}
}
for(let i=0, max=mails.length; i<max; i++)
{
mails[i].addEventListener('blur', function(e)
{
doLinkForMail(this.id, this.value);
},false);
window.addEventListener('DOMContentLoaded', function(e)
{
doLinkForMail(this.id, this.value);
}
} |