Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
Inserting an element after another with .after() does not fully work

In a webpage (from remote server where I have no access) I want to insert the page URL after a certain element, and after the webpage loads.

My solution is posted in the snippet below

var address = document.URL;
var pane = document.getElementsByClassName("heading--large")[0];
pane.after("<a href=\"" + address + "\">" + address + "</a>");

The page's URL is inserted, but only as text, not as a recognized element.

The URL is NOT active like the other links. Take a look at the snapshot I uploaded.

SnapShot

Why is the text in my anchor-element not rendering blue? Furthermore the "href" is not highlighted red like the other anchor elements are below.

Is ".after()" not the suitable function?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

In your code, you are inserting a string after the pane node as the Element.after documentation shows. If you want to use after, modify your code with document.createElement and modify the resulting elements to mirror your original intent

var address =
  "https://www.ghacks.net/2021/10/02/which-are-the-best-screenshot-extensions-for-chrome/";
var pane = document.getElementsByClassName("heading--large")[0];
var myLink = document.createElement("a");
myLink.href = address;
myLink.textContent = address;
pane.after(myLink);
<main class="post-list mt--60 mt--first--0">
  <h1 class="heading--large" style="color:#43414e;font-weight:400;"> Which are the best screenshot extensions for Chrome? </h1>
  <div class="text-small ghacks-links ghacks-links--smallunderline mt--10 mb--20" style="font-size: 14px;opacity:0.9;margin-top:10px;margin-bottom:20px;"> by Shaun on October 02, 2021 in Google Chrome extensions - Last Update: September 30, 2021 - 6 comments </div>
</main>

about 4 years ago · Juan Pablo Isaza Denunciar

0

.after() places nodes after a given element. A node is many things, but there's two types of nodes we usually are concerned with are elements and text. Your requirements are:

  1. Create an <a>

  2. Add href attribute with a pre-determined value to <a>*

  3. Add pre-determined text content to <a>*

  4. Place <a> after .heading--large*

    *#2, #3, and #4 can be in any order

.after() can only do #4, so here's a possibility if you really want to use .after():

  1. const A = document.createElement('a')
  2. A.href = address
  3. A.textContent = address
  4. pane.after(A)

const address = document.URL;
// This a modern equivalent
const pane = document.querySelector(".heading--large");
/*
This will safely render htmlString at a specfic location in relation to a given element.
*/
pane.insertAdjacentHTML("afterEnd", `<a href="${address}">${address}</a>`);

// Check to see if it's correct with .outerHTML property
console.log(document.querySelector("header").outerHTML);
<header>
  <h1 class='heading--large'>Chrome Extensions</h1>
</header>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda