• support@answerspoint.com

Can't change url in “a” tag in specific span with getElementsByClassName

689

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();

 

1Answer


0

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 2 years ago
  • B Butts

Your Answer

    Facebook Share