0

Applying target(_blank) attribute to all anchor tags with JS

In the below code, I want to remove the checkbox function and want to open all anchor tags with _blank attribute. Please remove the form also. Thanks! <script> function newTab(f) { var els = document.getElementsByTagName('a'); //read anchor elements into variable if(f.tab.checked == true) { //If the box is checked. for (var i in els) { els[i].setAttribute('target', '_blank'); //Add 'target="blank"' to the HTML } } else { // not checked... for (var i in els) { els[i].setAttribute('target', '_self'); //Add 'target="self" to HTML } } } //end function. </script> <form> <input type="checkbox" name="tab" onclick="newTab(this.form)" /> <label>Open Links In New Tab</label> </form> <a href="#">Test_Link</a>

9th May 2020, 8:11 AM
Mukesh Kaswan
Mukesh Kaswan - avatar
2 ответов
+ 1
Is this what you want? Let me know :)) https://code.sololearn.com/W5qXw5JHPq8W/?ref=app
9th May 2020, 9:08 AM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 1
Yes, it worked for me. Thanks!
9th May 2020, 9:53 AM
Mukesh Kaswan
Mukesh Kaswan - avatar