Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do people use jQuery for basic operations?

I am a JS programmer and I have been experimenting with jQuery a lot but have run into a couple puzzling aspects.

I feel like people use jQuery for much more than necessary. I really just want to know why picking jQuery may be better than using just pure JS.

I know it makes sense for webfx like the animate and fades but for things like adding event listeners it seems just as easy to use

obj = document.getElementByID(_ID_); obj.addEventListener("mousedown"...); 

An example of this is the answer I found on StackOverflow earlier today about performing an action for highlighted text. Get the Highlighted/Selected text

In the example linked in the answer at http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html

The guy uses the bind function to the document. Why use bind rather than addEventListener. Also with jQuery everything needs to be included in the .ready() method how is this better than (or why choose it over)

document.addEventListener('load', function () { ... }, false); 

There are other times I have seen jQuery used that puzzled me, I hope you guys can shine some light on it for me.

like image 568
austinbv Avatar asked Mar 21 '11 16:03

austinbv


People also ask

Why we are using in jQuery?

The main purpose of using jQuery is is to make it much easier to use JavaScript on your modern and smart website. It is highly recommended to have a basic knowledge of HTML, CSS, and JavaScript. jQuery was developed to save the time of developers by reducing the code.

Why should we use jQuery instead of plain JavaScript?

jQuery simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is easier to use compared to JavaScript and its other JavaScript libraries. You need to write fewer lines of code while using jQuery, in comparison with JavaScript.

Is jQuery still relevant in 2021?

6 Reasons Why We Still Use jQuery in 2021. jQuery has been around for over 10 years, which is a long time for a code library. It's still one of the most popular JavaScript libraries in web development. We love jQuery here at Atypic and still utilize it in our projects.


2 Answers

People use jQuery because it's simpler, easier, and more powerful, and because it helps them forget about IE.

To answer your specific questions:

  1. Otherwise, you need to call attachEvent for IE.
    Also, jQuery event handling has simpler syntax, and supports live events.

  2. jQuery does not require you to put everything in a ready handler; it's actually better to move your code to the bottom of the page and execute it immediately.
    Unlike document.addEventListener('load', ...), jQuery's ready event will not wait for images to load.
    Also, it works in IE, and it will still run your code even if the document already loaded.

like image 150
SLaks Avatar answered Sep 18 '22 21:09

SLaks


Well, on() is quite useful because addEventListener() is only supported from Internet Explorer 9 onwards.

The reverse is true for e.g. the mouseenter and mouseleave events: those are only supported by IE (so far), and jQuery emulates them in other browsers.

like image 23
Frédéric Hamidi Avatar answered Sep 19 '22 21:09

Frédéric Hamidi