Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

272
Visualizações
How can I convert specific text to link within a div node?

NOTE: The first requirement for this is that it not use jQuery. I also do not want to use .innerHTML if I can avoid it (even if that requires a bit more code)

I have divs within a page (multiple locations) that will be something like this:

<div class="p-user-content">John Smith current working on ticket LQ-1954</div>
    ... again, multiple locations ...
<div class="p-user-content">Sally Jones <a href="[something]">assigned</a> GM-3398 yesterday</div>

There will be simple text, and tags like <a> mixed in with the text as shown.

The following script successfully identifies all the "text nodes":

var el = document.getElementsByClassName('p-user-content');
for (var i in el) {
    if (typeof el[i].childNodes === 'undefined') continue
    for (var k in el[i].childNodes) {
        if (typeof el[i].childNodes[k] === 'function') continue
        if (el[i].childNodes[k].nodeType !== Node.TEXT_NODE) continue

        /**
         This successfully gets the nodeValue
         */
        console.log(el[i].childNodes[k].nodeValue)
    }
}

What I want to do is split the text node word-by-word, and then convert values like LQ-1954 and GM-3398 to anchor elements, and then replace the modified text and anchor links back into the existing div. I'm able to split the text up and do the matching part, but how would I

  1. build the nodes back with the new links and
  2. replace it back into the same div?
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use .replace

document.body.outerHTML = document.body.outerHTML.replace(/(LQ-1954)/g , '<a> $1 </a>')
about 4 years ago · Juan Pablo Isaza Relatório

0

If you must avoid manipulating html by changing the value of innerHTML, create a new element and replace the old element with it:

function addLinks(){
var el = document.getElementsByClassName('p-user-content');
for (var i in el) {
if (typeof el[i].childNodes === 'undefined') continue
let value = el[i].innerHTML.replace(/[A-Z]{2}-[\d]{4}/g, (match)=>'<a href="#">' + match + '</a>');
  let newDiv = document.createElement('div')
  newDiv.insertAdjacentHTML("beforeend", value);
  el[i].replaceChildren(newDiv)
}
}
<div class="p-user-content">John Smith current working on ticket LQ-1954</div>
    ... again, multiple locations ...
<div class="p-user-content">Sally Jones <a href="[something]">assigned</a> GM-3398 yesterday</div> 

<button onclick="addLinks()">Add Links</button>

Simple way using innerHTML:

function addLinks(){
var el = document.getElementsByClassName('p-user-content');
for (var i in el) {
if (typeof el[i].childNodes === 'undefined') continue
el[i].innerHTML = el[i].innerHTML.replace(/[A-Z]{2}-[\d]{4}/g, (match)=>'<a href="#">' + match + '</a>');
}
}
<div class="p-user-content">John Smith current working on ticket LQ-1954</div>
    ... again, multiple locations ...
<div class="p-user-content">Sally Jones <a href="[something]">assigned</a> GM-3398 yesterday</div> 

<button onclick="addLinks()">Add Links</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda