Jump to content

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


pstein

Recommended Posts

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

Therefore I use the following code:

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

Yes, the page URL is inserted, but only as text and not as recognized element.

The URL is NOT clickable (as the following other Links). Have a look at the following snapshot:

Why is in the inserted element "a" not blue and "href" not red as in the others "a" elements below?

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

insert-link-error.png

Edited by pstein
Link to comment
Share on other sites

It's best to use DOM objects instead of HTML strings when working in Javascript.

var pane = document.getElementsByClassName("heading--large")[0];

var link = document.createElement("a");
link.href = document.URL;

pane.after(link);

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...