How to translate from jquery to pure js?
Good day. Faced such a problem. How to quickly translate code from jquery to pure js? I have a large piece of code in jquery and manually rewrite it in js for a very long time, but I didn’t want to include the library so as not to load the project and it worked faster. Are there services or programs for translation?
4 Replies
I tried a few converters and they all had many issues, many just blindly convert
$()
into document.querySelector
, and on
into addEventListener
turning $(document).on("click", ".darkmode", function(){
into document.querySelector(document).addEventListener("click", ".darkmode", function(){
I think you will just need to go through it manually
However I think it's also an opportunity to improve things. jQuery relies on selecting elements every time they're needed rather than creating and passing elementsThis might be a good place to start:
https://gomakethings.com/series/converting-jquery-to-vanilla-js/
Converting jQuery to vanilla JS
Articles and live-coding videos exploring vanilla JS equivalents of common jQuery methods and plugins.
jQuery Method Articles append() children() addClass(), removeClass(), toggleClass() and hasClass() click() remove() next() jQuery Plugin Live-Coding Videos Lettering.js fitVids.js
You can create custom function very easily. You can also name a function with a dollar symbol.
What jquery does is make a wrapper object ( i think), so just add a property to an object referring to the element. And make methods changing the element, so you can :
ObjectName.methodName();
Just like jquery does it.
BTW this is just my thought, there might be better ones
Thank you all very much