JavaScript – How Does jQuery Work?

javascriptjquery

What exactly happens underneath (I know it changes the paragraph tag's HTML when the circular div is clicked) when I run this piece of code? Could you please explain the callback function as well.

<script type="text/javascript">
    $("#circle").click(function() {
        $("p").html("You clicked the circle");
     }
</script>

I am aware of the observable changes that take place. What exactly happens underneath when I run this code? Does jQuery convert the resulting code to JavaScript? It would be great if someone could give me a blow by blow account.

This is a part of my question that I had earlier posted on StackOverflow. It got down voted for being too broad.

Thanks 🙂

Best Answer

A high-level dissection of the code posted:

<script type="text/javascript">

Html-element to embed a script in it

$("#circle")

$ is the jQuery alias for getting the jQuery object which wraps almost all of the stuff jQuery can do. In this case we simply invoke the jQuery object with syntax similar to that of a constructor. This is shorthand for "pass in a css-selector and return all matched DOM elements wrapped as jQuery elements". So we select the element in the DOM with id=circle

  .click(

This is (one of many) jQuery-ways to say when the element(s) - the previous selector could have returned a ton of elements - is clicked run the function passed into me. This function is called a callback-function. Basically subscribing to the onclick event of the DOM element.

    function() {

.. the callback passed in...

      $("p")

Similar to the $("#circle")-statement. Select all paragraph-elements in the DOM as jQuery elements. (<p></p>)

       .html("You clicked the circle");

For all p-elements previously selected set the inner html of this elements to be "You clicked the circle". Note; You should almost never want to do this for security reasons - use .text() instead.

   }

.. close the call back ..

)

.. Honorably mention; You forgot to close the click()-function :) The code as-is will still work though, since javascript is forgiving (For better or for worse, that's another story)

</script>

.. closing the script-tag..

As it has been stated, this is a really broad question even though it does not look like it. For all the above paragraphs you could probably write a page of what it really does under the hood, but that's the gist of it.

For your question "Does jQuery convert the resulting code to JavaScript?". Not really. jQuery is implemented in javascript, so you can simply think of it as a wrapper library. jQuery serves two main purposes: 1) Providing an API that works regardless of browser types. So for example, if Safari has some quirks working with the onclick-event but firefox follows the spec, jQuery will take care of this under the hood for you (Not the the Safari/Firefox example is purely made up, none of those should have quirks). And 2) provide QoL-improvements such as CSS-selectors, although those have since been implemented in ES5 or ES6, can't remember which. When jQuery came out you had to call document.getElementById() or a similar DOM-method to select elements.

At the risk of starting a religious war, jQuery has nowadays largely served its purpose. Most of the stuff that made jQuery popular is now implemented in recent ECMAScript-versions and most browsers are now updated to at least include the basic stuff. Personally, I only use it because it's a familiar API, as I used to work extensively with it, but it's not "as required" to use it as it once was.

Hope it helps :)

Related Topic