0

Why am I unable to create an alert when a button is disabled. But when the button is enabled I can get the alerts?

$(document).on('click', '#printpage', function() {
  alert('clicked');
if ($("#printpage").is(":disabled")) {
   alert("Disabled");
 } else {
   alert("enabled");
 }
});
David Brierton
  • 6,977
  • 12
  • 47
  • 104

2 Answers2

2

Disabled button is not clickable. If you want alert, use class instead of disabled attribute and stylize it with css.

$(document).on('click', '#printpage', function() {
if ($("#printpage").is(".disabled")) {
   alert("Disabled");
 } else {
   alert("enabled");
 }
});
.disabled {
    cursor: not-allowed;
    opacity: .5;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="disabled" id="printpage">Print</button>
Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
0

You could also float an invisible element over the top of the button and put an onclick event on that. Obviously, you'd need to remove the element when you enabled the button. However, you'd always need to disable/enable the button through a function rather than simply setting the disabled attribute on the button itself.

thefuzzy0ne
  • 479
  • 3
  • 10