Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Bad practices [closed]

Tags:

What are the JQuery bad/worst practices you have seen, written or thing should be avoided?

like image 643
piyer Avatar asked Jan 15 '10 07:01

piyer


People also ask

Is using jQuery a bad practice?

Nothing is inherently wrong with jQuery, only how you use it. It is a library that makes some JavaScript features easier to use, and it can save time and headaches in lots of projects. The problem is that anything that's easy to use is misused by beginners.

Is it bad practice to mix JavaScript and jQuery?

Yes, they're both JavaScript, you can use whichever functions are appropriate for the situation. Show activity on this post. That kind of lower-level hacking around the DOM, messing with the inline handlers, and cross-browser quirks thereof, is precisely the reason jQuery was born.

Is it a good idea to use jQuery in React?

It's a bad practice because React uses a concept called a Virtual DOM instead of a real DOM. And React isn't aware of the changes made outside of this Virtual DOM. When you use jQuery or any other library that manipulates the DOM, React gets confused.


1 Answers

A thing you should avoid is using the "easy to use" selectors in every line once again, because the JavaScript implementation of selectors is not that efficient. Of course, the jQUery guys are optimizing it, however I think you should use it as little as possible.

So, this is a bad practice.

$("li ul span").show();
$("li ul span").toggleClass("bubu");

Chaining is good

$("li ul span").show().toggleClass("bubu");

And remembering things in a local variable is also not bad:

var allspans = $("li ul span");
allspans.show();
allspans.toggleClass("bubu");
like image 126
naivists Avatar answered Jan 03 '23 17:01

naivists