Vanilla Javascript Selectors
Everytime I start a new project I try to evaluate my needs for javascript and decide if I am going to use any third party library or not.
I am not talking about prototyping, we can use everything we want to get a prototype fast and easy, but what about our production code?
I know there are some lightweight javascript libraries out there but trust me, if you ‘re just using a library to select elements then something is not right.
And it’s slower too!
So, this is a post about using your own selectors!
Get Elements By id
This is an easy one, just call document.getElementById('your_id');
and you are ready to go!
If you think this is boring we can create a simple wrapper function to help our coding.
Get Elements By Class
Getting elements by class is fairly easy too, but we need to write some code to iterate through the selected elements.
That’s all for now.
Within a few lines of code we saved ourselves a 40kb - 80kb library request and browser memory.