Add Target="new" to all links with JavaScript

Add Target="new" to all links with JavaScript

by Nirvanachain

HTML

<div id="myDiv">
    <a href="http://google.com">Link 1</a>
    <a href="http://3M.com">Link 2</a>
    <a href="http://IBM.com">Link 3</a>
</div>

JavaScript

// Find all the anchors you want to modify
var anchors = document.getElementById('myDiv').getElementsByTagName('a'),
    i = anchors.length;

// Add the target to each one
while(i--) {
    console.log(anchors[i]);
    anchors[i].target = "new";
};