n.y
n.y

Reputation: 3490

How to disable `accesskey` when the element is hidden?

Please see the below code:

$('div').click(function(){
   alert("Shortcut worked although element was hidden!");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div hidden accesskey="1">
    Some things...
</div>

<p>
  Focus on me and then 
  </br>
  <b>press [Alt + 1] please!</b>
</p>

How can I to disable accesskey when the element get hide?

Upvotes: 2

Views: 1017

Answers (2)

Ted
Ted

Reputation: 4067

You can check whether the div is hidden in the click event:

$('div').click(function(event) {

  if (!$(this).is(':hidden')) {
    alert("Shortcut worked although element was hidden!");
  } else {
    event.preventDefault();
  }

});

https://jsfiddle.net/89b2jtmw/1/

Upvotes: 1

Mohit Tanwani
Mohit Tanwani

Reputation: 6628

I don't think there is a way to disable the accesskey, you just need to add / remove that as per you need

if(!$('div').is(":visible"))
{
   $('div').removeAttr('accesskey');
}
else
{
   $('div').attr('accesskey', '1');
}

if(!$('div').is(":visible"))
{
  $('div').removeAttr('accesskey');
}
else
{
  $('div').attr('accesskey', '1');
}
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div hidden accesskey="1">
    Some things...
</div>

<p>
  Focus on me and then 
  </br>
  <b>press [Alt + 1] please!</b>
</p>

Upvotes: 1

Related Questions