Home > Software design >  How to display another element when hover disabled button?
How to display another element when hover disabled button?

Time:06-03

I have a disabled button and I want to display a message in the other side of the page (NOT A CHILD ELEMENT) when I hover this button. How to do that? I already tried using jquery and CSS but it doens't work:

#deleteManagerWarning{
  display: none;
  color: red;
  float: right;
}

#disabledCloseBtn:hover   #deleteManagerWarning{
  display: block;
}

and

  $("#disabledCloseBtn").hover(function(){
    $("#deleteManagerWarning").css("display", "block");
  });

This is the html button:

<button type="submit" 
 
id="disabledCloseBtn" 
name="disabledCloseBtn" 
disabled 
aria-label="Close">

This is the html message:

<span id="deleteManagerWarning">Unable to delete Manager</span>

CodePudding user response:

If the two elements are far separated from each other in the markup, CSS won't help you-- you need a relationship like descendent or adjacent. In this case, your jQuery would be working if the button were not disabled:

$(document).ready(() => {
 $("#disabledCloseBtn").hover(function(){
    console.log('hover');
    $("#deleteManagerWarning").css("display", "block");
  });
 });
#deleteManagerWarning {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <button type="submit"  id="disabledCloseBtn" name="disabledCloseBtn" aria-label="Close">
  Close
</button>
</div>
<div>
  <p>Dummy content</p>
  <p>Dummy content</p>
  <p>Dummy content</p>
  <span id="deleteManagerWarning">Unable to delete Manager</span>
</div>

However, it appears that jQuery does not (or cannot) fire the hover event for a disabled button-- the following example is exactly the same as the first except the button is disabled:

$(document).ready(() => {
 $("#disabledCloseBtn").hover(function(){
    console.log('hover');
    $("#deleteManagerWarning").css("display", "block");
  });
 });
#deleteManagerWarning {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <button type="submit"  id="disabledCloseBtn" name="disabledCloseBtn" disabled aria-label="Close">
  Close
</button>
</div>
<div>
  <p>Dummy content</p>
  <p>Dummy content</p>
  <p>Dummy content</p>
  <span id="deleteManagerWarning">Unable to delete Manager</span>
</div>

In this case, you have a few alternative options you can employ:

  1. Fake the disabled state on the button: reduce the opacity, remove click handlers, make sure you update the ARIA messaging to report the button as disabled. (You could also fake the button entirely using a <div> and wiring up all the accessibility and interactivity, but this would be much more difficult and to a similar effect). This may actually be more accessible, because a disabled button isn't focusable by keyboard.
  2. Use a different hover target: Instead of the button, try using the wrapper around the button, or float something invisible over the button.

Here is an example faking the disabled state:

$(document).ready(() => {
  $("#disabledCloseBtn").hover(
    function() {
      $("#deleteManagerWarning").css("display", "block");
    },
    function() {
      $("#deleteManagerWarning").css("display", "none");
    },
  );
});
#deleteManagerWarning {
  display: none;
}

.disabled {
  opacity: 0.5;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <button type="submit"  id="disabledCloseBtn" name="disabledCloseBtn" aria-label="Close">
  Close
</button>
</div>
<div>
  <p>Dummy content</p>
  <p>Dummy content</p>
  <p>Dummy content</p>
  <span id="deleteManagerWarning">Unable to delete Manager</span>
</div>

Just make sure you are figuring out a way to communicate that it is disabled to assistive technologies -- and remember, content that is popping into the existence on the other side of the page probably needs to be appropriately announced to screen readers.

CodePudding user response:

Problem

It appears that JavaScript and jQuery do not detect disabled tags (I learned something today).

CSS has no problem:

Figure I

<button class='A'>A</button>
<output class='A'>HELLO WORLD!</output>

Figure II

button.A:hover:disabled   output.A {
  display: inline-block;
} 
/*            
  • Related