0

I have anchor links throughout a table I am working on.

<div id="table1">
<td>See example <a href="#john">john</a>; <a href="#julie">julie, an</a>; <a href="#sam-ham">sam-ham</a>; <a href="#state-names">state names</a>.</td></div>

These links need to be opened in a new tab by adding the following code - target="_blank". As a result, the link should look like this

See example <a href="#john" target= "_blank">john</a>;

Would it be possible to add the code without manually updating hundreds of links?

Please note that I aslo have other links on the page

magicsinger
  • 127
  • 8
  • There’s [``: The Document Base URL element](//developer.mozilla.org/en/docs/Web/HTML/Element/base#attr-target). Keep in mind that forcing a specific way to open links is bad UX. – Sebastian Simon Feb 14 '23 at 21:13
  • would add code to all the links on the page? I want to add the code only on a table. – magicsinger Feb 14 '23 at 21:19
  • @magicsinger `document.getElementById('table1').querySelectorAll('a').forEach(el => el.target = "_blank");` – Ryan Wilson Feb 14 '23 at 21:19
  • I think this will add to all links on the page and not just the table?
    – magicsinger Feb 14 '23 at 21:20
  • 1
    @magicsinger I updated my comment to provide you what you want. It will first get the element with id `table1`, then get all descendant anchors of the table. – Ryan Wilson Feb 14 '23 at 21:21

0 Answers0