Skip to content

Instantly share code, notes, and snippets.

@nezamy
Last active July 15, 2017 20:32
Show Gist options
  • Save nezamy/9f5476c602cb807e4378ec14bbd341bd to your computer and use it in GitHub Desktop.
Save nezamy/9f5476c602cb807e4378ec14bbd341bd to your computer and use it in GitHub Desktop.
Javascript shortcuts

Javascript

Javascript sortcuts

$('class or id or tagname', 'event', 'callback');    
document.addEventListener('DOMContentLoaded', function () {
    // If click toggle class
    $('.navbar .burger', 'click', function($e){
        $('#'+$e.dataset.target).classList.toggle('active');
        $e.classList.toggle('active');
        $('body')[0].classList.toggle('menuactive');

    });
    
    //Triger click
    setTimeout(function(){
        $('.navbar .burger', null, function($e){
            return $e.click();
        })

    }, 1000);
});
var $byID = document.getElementById.bind(document);
var $byAll = document.querySelectorAll.bind(document);
var $byTag = document.getElementsByTagName.bind(document);
var $ = (elm, event, call) => {
var $selecetd;
var type = elm.substr(0, 1);
if(type == '#'){
$selecetd = $byID(elm.substr(1));
if(event){
$selecetd.addEventListener(event, (e) => {
return call(e);
});
}else{
if(call){
return call(e);
}else{
return $selecetd;
}
}
}
if(type == '.'){
$selecetd = $byAll(elm);
}else{
$selecetd = $byTag(elm);
}
var $elm = Array.prototype.slice.call($selecetd, 0);
if ($elm.length > 0) {
$elm.forEach(function ($el) {
if(event){
$el.addEventListener(event, (e) => {
return call($el, e);
});
}else{
if(call){
return call($el);
}else if(type == '.'){
return $byAll($el);
}else{
return $byTag(elm);
}
}
});
}
return $selecetd;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment