html - I need to make a JavaScript function that will return DOM elements given a CSS selector w/o jquery -


html

javascript have far

 var $= function(selector){  var elements = []; if (selector == "#some_id"){   elements.push( document.getelementbyid("some_id")); }else if (selector == '.some_class'){   elements= document.getelementsbyclassname("some_class"); } else if (selector == "div"){   elements = document.getelementsbytagname("div"); }else if (selector == "img"){   elements.push(document.getelementsbytagname("img")); } console.log(elements); return elements; }; 

having problem getting nested selectors : ("img.some_class") , ("div.some_class#some_id")

have tried queryselector or queryselectorall?

here 2 simple examples:

var el = document.queryselector(".myclass"); var matches = document.queryselectorall("div.note, div.alert"); 

see here more information: https://developer.mozilla.org/en-us/docs/web/api/document.queryselectorall

a simple simulation of jquery selector:

$ = function (selector) {   return document.queryselectorall(selector); } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -