javascript - Javscript document event on certain css class -


i'm used writing in jquery selecting class, following can't quite code right. lives on every page , should intercept links class 'download-link'. following works links. want target download-link css.

 document.onclick = function (e) {                 e = e ||  window.event;                 var element = e.target || e.srcelement;                  if (element.tagname == 'a') {                     window.open(element.href, "_blank", "location=yes,toolbar=yes,toolbarposition=top");                     return false;                  }              }; 

i can't quite work out selector if statement change element.tagname element.class or similar.

heres last thing tried

 document.getelementbyid("download-link").addeventlistener("click", function(e) {         window.open(e.href, "_blank", "location=yes,toolbar=yes,toolbarposition=top"); return false;         e.preventdefault();     }); 

you mention

should intercept links class 'download-link'

though use .getelementbyid(). can use .queryselectorall() selector ".download-link" , nodelist.prototype.foreach() perform task, see foreach method of node.childnodes?. example, attach event listener, each ".download-link" element

document.queryselectorall(".download-link") .foreach(function(element) {   element.addeventlistener("click", function(event) {   // stuff   }) }) 

if nodelist.prototype.foreach() not defined @ browser can use for loop achieve same result

for (var = 0, nodes = document.queryselectorall(".download-link");       nodes && < nodes.length; i++) {         nodes[i].addeventlistener("click", function(event) {         // stuff         }) } 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -