Daniel
Daniel

Reputation: 4342

find all href based on class

I am trying to find all <a> tags with a class of a_tv and based on that get their href attribute value. I have the following code, but it returns undefined. What am I doing wrong?

var hrefs, randomHref;

hrefs = Array.prototype.filter.call(document.getElementsByTagName("a"), function(node) { 
    return node.class.indexOf("a_tv") === 0;
}).map(function(node) {
    return node.href;
});

randomHref = hrefs[Math.floor(Math.random() * hrefs.length)];
console.log(randomHref);

Upvotes: 0

Views: 156

Answers (1)

Andreas
Andreas

Reputation: 21881

Its className not class

hrefs = Array.prototype.filter.call(document.getElementsByTagName("a"), function(node) { 
    return node.className.indexOf("a_tv") === 0;
}).map(function(node) {
    return node.href;
});

Upvotes: 3

Related Questions