0

I´m trying to add target=“_blank” to all the links within the divs with an specific class name. I know how to do it with an ID:

window.onload = function(){ var anchors = document.getElementById('link_other').getElementsByTagName('a'); for (var i=0; i<anchors.length; i++){ anchors[i].setAttribute('target', '_blank'); } } 

But i´m trying to replicate the same, using classes instead of IDS. Any ideas of a how to do this without jquery?.

Thanks in davanced!

1

4 Answers 4

2

You can use querySelectorAll() and include a CSS selector. So if your class name is link-other:

document.querySelectorAll('.link-other a') .forEach(function(elem) { elem.setAttribute('target', '_blank'); }) 
<div class="link-other"> <a href="http://wikipedia.com">Wikipedia</a> <a href="http://google.com">Google</a> </div>

Sign up to request clarification or add additional context in comments.

5 Comments

@user2965583 You should accept the most helpful answer.
This will not work as .forEach is not a method on a NodeList.
@andlrc You are mistaken. forEach() is a prototype method of Array. querySelectAll() returns an instance of Array. Click on Run code snippet to see that this does indeed work. For more information see developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
@Martin: yet MDN documentation contradicts you. document.querySelectorAll() returns a NodeList according to the DOM spec.
@Martin: perhaps you have extended the NodeList prototype?
0

Use querySelectorAll and loop just like you did.

var anchors = document.querySelectorAll(".link_other a"); 

Or you can use getElementsByClassName and nested loops.

var parents = document.getElementsByClassName(".link_other"); for (var i = 0; i < parents.length; i++) { var anchors = parents[i].getElementsByTagName("a"); for (var j = 0; j < anchors.length; j++) { anchors[j].setAttribute('target', '_blank'); } } 

Comments

0

You can use document.querySelectorAll() which takes a css expression:

var anchors = document.querySelectorAll('.my_class a'); for (var i=0; i<anchors.length; i++){ anchors[i].setAttribute('target', '_blank'); } 

Or (ab)using the array prototype:

[].forEach.call(document.querySelectorAll('.my_class a'), function(el) { el.setAttribute('target', '_blank'); }); 

You should also consider using addEventListener instead of window.onload:

window.addEventListener('load', function() { // ... }); 

Or the more appropriate:

document.addEventListener('DOMContentLoaded', function() { // ... }); 

You can also use the old school <base> element which will can set defaults for all a tags:

var base_el = document.createElement('base'); base_el.setAttribute('target', '_blank'); document.head.appendChild(base_el); 

Comments

0

To achieve your expected result use below option

 var addList = document.querySelectorAll('.link_other a'); for(var i in addList){ addList[i].setAttribute('target', '_blank'); } 

Codepen - http://codepen.io/nagasai/pen/QEEvPR

Hope it works

2 Comments

Updated the codepen with additional URLs with class link_other and few links without link_other class .And updated class as "link_other" to match with your code..Please find updated codepen URL below codepen.io/nagasai/pen/QEEvPR Hope this works for you :)
cool...can you mark as this question as answered or vote if possible :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.