javascript - Remove items from an HTML collection based on another HTML collection -


i'm trying crete 3 html collections containing links on page, can attach 3 separate function each categories of links. first html collection "header links", second "footer links" , third "all other links". need attach link tracking functions , other elements well.

creating first 2 collections easy can document.getelementbyid('header'); , document.getelementbyid('footer'); , this.getelementsbytagname('a'); however, getting third collection of "all other links" bit more tricky. there isn't clean div contains "middle" of page, , there links outside header , footer difficult single out. wish alllinks = document.linnks, , filter out of links present in first , second html collections.

any way ? ideally avoid loading more libraries , pure js welcome

thanks !

you can turn node lists arrays, use filter() pull out links in 1 of other lists:

var hdr = document.getelementbyid('header'); var hlinks = arrayof(hdr.getelementsbytagname('a'));  var ftr = document.getelementbyid('footer'); var flinks = arrayof(ftr.getelementsbytagname('a'));  var others = arrayof(document.getelementsbytagname('a')). filter(   function(element) {     return (hlinks.indexof(element) < 0) && (flinks.indexof(element) < 0);   } );  function arrayof(nodelist) {   var result = [];   ( var = 0; < nodelist.length; ++i )     result.push(nodelist.item(i));    return result; } 

example: http://codepen.io/paulroub/pen/ikebh

if need support older browsers lack array.prototype.filter(), include code this mdn page implement when needed.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -