Can't change url in “a” tag in specific span with getElementsByClassName
I want all the (urls)hrefs from <a>
tags in specific span with class"post-body entry-content" to change to specific url, here's my code i did put it in the end of the body tag.. put it seams not working and i cant get what im doing wrong cause theres not error in console.(im very noob in Javascript)
Basically im trying to change every url in span with "adfly in front".
Here's my code:
function links(){
var mylink = "http://naukrighar.in/000/";
var els = document.getElementsByClassName('post-body entry-content')[0];
var links = els.getElementsByTagName('a'),
len = links.length;
while( len-- ){
links[len].href = mylink + links[len].href;
}
}
links();
Javascript
- asked 9 years ago
- Sunny Solu
1Answer
What's returned from getElementsByClassName
is an "array like object" which does not in turn have getElementsByTagName
Instead, try querySelectorAll("a .post-body.entry-content")
(note: space between a
and .
, no space between body
and .
)
And loop over those results
See https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
- answered 8 years ago
- B Butts
Your Answer