'I load the full jQuery library, but I only use Ajax and selectors. Better shorter code?

I created a star-rating voting system based in jQuery, I load the full jQuery JS library, but I found that I only two features of it:

  • the simple Ajax method: $.ajax({url:"dosomething.php",cache:0,data:n,success:function(t)
  • and the selectors: $(document).ready(function()... $(".stars").hover(function... $(this).prevAll()...

I'm not a JavaScript expert, and I wondered if I can substitute the simple Ajax method and the selectors by short JavaScript code.

Thank you very much.



Solution 1:[1]

You don't have to use jQuery as there are alternatives to many functions jQuery used to perform.

For ajax your can use fetch API. For selectors you can use querySelector and querySelectorAll.

Another alternative is to use a lightweight library like Zepto.js which has the same syntax as jQuery. You can choose which module you want to use.

Finally, you can check http://youmightnotneedjquery.com/ for specific jQuery alternatives.

Solution 2:[2]

Worth noting that pages may need to render using HTML returned as JavaScript in certain cases. XMLHttpRequest cannot return HTML as script. jQuery's AJAX can.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kalimah
Solution 2 Ryan Allen