Skip to main content

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. jQuery: get the highlighted 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.



Source: Tips4all

Comments

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

    To answer your specific questions:


    Otherwise, you need to call attachEvent for IE.
    Also, jQuery event handling has simpler syntax, and supports live events.
    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.

    ReplyDelete
  2. Well, bind() 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.

    ReplyDelete
  3. The biggest reason for me is cross browser compatibility, especially with event handling.

    ReplyDelete
  4. The idea behind jquery is "Write less, do more".

    With trivial examples the difference in the amount of code written is small, but as you start to write more complicated stuff the power of jquery becomes apparent.

    There's also a lot of cross-browser stuff built into jQuery, which means you have to worry less about browser-specific code.

    ReplyDelete
  5. Because the jQuery developers are way smarter than I am, and will often implement a more efficient algorithm to do what I'm trying to do.

    ReplyDelete
  6. Here is why I would do things like that..

    I have more confidence in jQuery being platform-independent than I do with mere JavaScript. For that reason, I'm tempted to use jQuery as much as I can. And I think jQuery is good and stable enough of a platform to abstract away some of the browser-specific complications that way.

    ReplyDelete
  7. cause it's Cross-Compatible and well supported (think about XHR requests)... but in some project I'd find better to use MooTools instead of JQuery in cause of a lack of "assets" method.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.