dumka.js

dumka.js is an ultra-lightweight JavaScript library of just 8 functions providing 8 common utility functions for DOM manipulation and event handling.

The library is so small you can read and understand the entire codbase in few minutes, not just what it does.

Library Functions

Examples

1. Selecting Elements

HTML:

<div id="example" class="sample">Hello World</div>

JavaScript:

const element = _d.id("example");
console.log(element.textContent); // Outputs: Hello World
        

2. Event Handling

HTML:

<button id="clickMe">Click Me</button>

JavaScript:

_d.when("clickMe", "click", () => alert("Button Clicked!"));
        

3. Inline Messages

HTML:

<input id="username" placeholder="Enter your username" />

JavaScript:

_d.inform(_d.id("username"), "Username is required", "error");
        

What does inform do?

The inform function creates a temporary inline message next to the specified element. The message can be styled based on the type:

The message also automatically disappears after 5 seconds.

Example:

_d.inform(_d.id("username"), "This field cannot be empty", "error");
        

4. Executing Code When Document is Ready

JavaScript:

_d.docReady(() => {
    console.log("Document is fully loaded and parsed!");
});
        

dumka.js code

            
                const _d = {
                    id: function id(i) { return document.getElementById(i) },
                    cl: function cl(c) { return document.getElementsByClassName(c) },
                    tag: function tag(tg) { return document.getElementsByTagName(tg) },
                    first: function first(sl) { return document.querySelector(sl) },
                    all: function all(sl) { return document.querySelectorAll(sl) },
                    when: function when(i, ev, fn){
                        const el = document.getElementById(i);
                        if (el) el.addEventListener(ev, fn);
                    },
                    inform: function inform(el, msg, type) {
                        const message = document.createElement("span");
                        message.className = "message";
                        message.textContent = msg;
                
                        switch (type) {
                            case "error":
                                message.style.background = "LightSalmon";
                                break;
                            case "warning":
                                message.style.background = "yellow";
                                break;
                            case "info":
                                message.style.background = "PaleTurquoise";
                                break;
                            case "success":
                                message.style.background = "LightGreen";
                                break;
                            default:
                                message.style.background = "white";
                        }
                
                        el.insertAdjacentElement("afterend", message);
                        setTimeout(() => message.remove(), 5000);
                    },
                    docReady: function docReady(fn) {
                        if (document.readyState !== "loading") {
                            fn();
                        } else {
                            document.addEventListener("DOMContentLoaded", fn);
                        }
                    },
                };