G
G
Good Samaritan2018-02-19 13:41:45
JavaScript
Good Samaritan, 2018-02-19 13:41:45

How to create a neighbor (tag)?

I have a lot of links on the page like <a href="some" class="tab-category">some</a>
How can I create another link next to each link like <a><img></a>?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stalker_RED, 2018-02-19
@djamali

document.querySelectorAll('.tab-category').forEach(
  link => link.insertAdjacentHTML('afterend', '<a><img></a>')
)

I
Interface, 2018-02-19
@Interface

Either on css as written above, or on js, for example api.jquery.com/insertafter

A
Andrey Tsvetkov, 2018-02-19
@yellow79

Roughly something like this

var anchors = document.querySelectorAll('a.tab-category');
for(var i = 0; i < anchors.length; i++) {
  var a = document.createElement("a");
  a.appendChild(document.createElement("a"));
  anchors[i].parentNode.insertBefore(a, a.nextElementSibling);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question