Blog

What are widgets in jQuery & how are they different from regular objects/functions?

Table of Contents

Javascript has grown to be the most used programming language, for the web. It “gives life to a webpage”. But since Javascript is a very loosely-typed language, code quality and maintainability can turn out to be a huge issue.

Fortunately, people have come up with tools like requireJS and jQuery, to make coding in Javascript easier, by dividing your code in modules.

In this article, we’re going to discuss widgets in jQuery, a way to modularise our Javascript code, for most efficiency, scalability, and maintainability.

jQuery provides a way to bind certain functionalities to a particular element, so that instead of selecting the DOM element from within our function, we directly receive a reference to the DOM element, in which our widget was instantiated, inside our function. This concept is called widgets in jQuery and is useful in writing maintainable code, as well as instantiating multiple instances of the same widget, within different DOM elements.

Feel confused? Don’t worry. I did too, at the beginning. Let’s take a simple example to explain how widgets are useful.

Task

There are multiple DIVs, having data-urlattributes, containing URL of data, which we should fetch and display inside the DIV.

<div class="dynamic" data-url="http:://www.example.com/something"></div>
<div class="dynamic" data-url="http:://www.example.com/something-else"></div>
<div class="dynamic" data-url="http:://www.example.com/i-really-need-to-be-more-creative"></div>

Solution

Now in a normal situation, we would write some messy Javascript code like this

<script>
    var fetchData = function () {
        var selector = ".dynamic";
        var loadingHtml = "loading...";
        var elements = $(selector);
        elements.each(function (key, element) {
            element = $(element);
            $.ajax({
                url: element.data('url'),
                method: "GET",
                success: function (response) {
                    element.html(response);
                }
            });
        });
    };
    fetchData();
</script>

But using widgets, first we’ll develop a widget.

<script>
    $.widget('codilar.fetchData', {
        options: {
            loadingHtml: "Loading..."
        },
        _create: function () {
            var self = this;
            self.element.text(this.options.loadingHtml);
            $.ajax({
                url: self.element.data('url'),
                method: "GET",
                success: function (response) {
                    self.element.html(response);
                }
            });
        }
    });
</script>

And then we can call this widget on any DOM element, like this

$('.dynamic').fetchData();

So a widget is basically a JS object having 2 mandatory fields, options and _create. The _createfunction of your widget is called automatically, and the options object usually contains default configuration parameters for your widget, which can be overridden while calling the widget. Also, the this.elementfield is the current jQuery DOM element on which the widget is instantiated.

So since we’ve placed the loadingHtmlinside the options, we can override that while instantiating the widget, like this

$('.dynamic').fetchData({
    loadingHtml : "Widgeting..."
});

So that’s how you create, instantiate and modify a basic jQuery widget. Hope you liked this blog.

jquery

Additional resources

Magento jQuery widgets

jQuery widget coding standard

Previous tutorial

How to create a “HELLO WORLD” module in Magento 2?

Do let me know in the comment section below about what you want my next tutorial blog to be about. See you till next time!

Picture of Arijit Panigrahi

Arijit Panigrahi

I am experienced in Magento development specialising in PHP, HTML, jQuery, and PWA projects with expertise in GraphQL, algorithm design, and database design, I have delivered high-quality solutions for diverse clients. I am experienced in algorithm design, optimizing application performance, and database efficiency. With a commitment to professionalism and a passion for continuous learning, remain at the forefront of web development innovation.

You May Also Like

Latest Blogs

Magento Development Company

Let’s talk

Our Offices

DTECH, Techno Hub 1, Dubai Silicon Oasis Authority, United Arab Emirates – Dubai – United Arab Emirates

Singapore

Codilar Digital Pte Ltd, 68 Circular Road, #02-01, 049422, Singapore

India

Bengaluru

7th Floor, Jupiter Block ,Prestige Tech Park, Kadubeesanahalli, Bellandur Amanikere, Bengaluru, Karnataka 560103

Calicut

SBC No 4 & 6 upper basement, Sahya Building
KSITIL SEZ, Cyberpark Kozhikode Park Rd, Nellikkode, Kozhikode, Kerala 673016

Kolkata

Astra Towers, ANO -523 ,North Block, Action Area IIC, Newtown, Kolkata, West Bengal 700135

Ahmedabad

Codilar Technologies, Connekt, 13th Floor, Gala Empire, Opposite T.V. Tower, Drive In Rd, Memnagar, Ahmedabad, Gujarat – 380052

Oman

Building No. 2/796, Way No. 43, Block No. 336, Al Khud 132, Muscat, Oman

Codilar

© Copyright Codilar 2025. All Rights Reserved. Privacy Policy

Send Feedback

Request PWA Demo