Skip to content Skip to sidebar Skip to footer

GetElementsByTagName Specific Links Only

I'm trying to use Anarchy Media Player on my site but had to change code a bit because my media files/urls are not in standard format. It pulls the video files now but javascript i

Solution 1:

Not sure why you're not just doing this:

var all = document.getElementsByTagName('a');
for (var i = 0, len = all.length; i<len; i++) {
    if(all[i].idName=="video" && all[i].className!="amplink") {
        // Add player code...
    }
}

That should work for you. Note that I stored the value of the array length so you don't have to look it up every iteration. Minimal, but does increase performance slightly.

You were also setting idName to video when you did idName="video" in your if statement, not comparing (that will always return true, which was your problem). Use the double equals operator (==) or the triple equals operator (===) to compare values.


Solution 2:

Are you sure you don't mean if (o.className == "amplink")?

Besides that what you have is fine (except for the o.idName="video" bit which always returns true): http://jsfiddle.net/VpMVe/


Solution 3:

you can get elements using querySelectorAll, but it's not supported in older browsers (that would be older then IE8, FF3.5, Opera 10, Chrome 4 and Safari 3.1). It's similar to targetting elements using CSS selectors (and jQuery). here's a demo getting all links with class video to turn red.

function get(selector) {
    return document.querySelectorAll(selector)
}

(function() {
    //get all <a> with class "video"
    var video = get('a.video');

    //for each, turn them red
    for (var i = 0; i < video.length; i++) {
        video[i].style.color = 'red';

        //in your case, do whatever you want for each target here

    }
}())​

Solution 4:

There is no idName attribute. It's just o.id. And there can only be one id, so that's likely to cause some issues. Also you're assigning it, rather than comparing for equality which always evaluates to true. If you have more than one class, e.g. class="amplink someOtherClass" != amplink will evaluate to true. Better to do a match on the className. Like this:&& !o.className.match(/ampLink/)

Make a video on another class and do matches instead.


Post a Comment for "GetElementsByTagName Specific Links Only"