Created
February 6, 2024 19:42
-
-
Save omar2205/846bff7081c34b703eacb32af73b38d2 to your computer and use it in GitHub Desktop.
loading button jquery plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function ($) { | |
$.fn.button = function (action) { | |
const $btn = $(this) | |
let alt_text = 'Loading' | |
if (action === 'loading') { | |
let original_icon = false | |
$btn.attr('data-oringinal-text', $btn.text()) | |
if ($btn.has('i').length) original_icon = $btn.find('i').attr('class') | |
$btn.attr('data-oringinal-icon', original_icon) | |
$btn.html(`<i class="fas fa-spinner fa-spin"></i> ${alt_text}`) | |
$btn.attr('disabled', true) | |
} else { | |
$btn.text($btn.attr('data-oringinal-text')) | |
const icon_class = $btn.attr('data-oringinal-icon') | |
if (icon_class) { | |
const icon = document.createElement('i') | |
icon.classList.add(...icon_class.split(' ')) | |
$btn.prepend(icon) | |
} | |
$btn.removeAttr('disabled') | |
} | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment