最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Tab on disabled input - Stack Overflow

programmeradmin7浏览0评论

I am implementing progressive UI disclosure pattern in my application. Using which I am disabling the next elements. So based on input of one element the next element is enabled.

But I have a problem is since the next element is disabled, the tab from the current element is taking the focus to the end of document or the tab header when tab out. As the progressive enables the element after the tab out, while this was happening the next element was not enabled so tab was lost outside the document.

So my requirement is to enable tab on the disabled elements and also on mobile/tablet devices the click events should at least be registered on the disabled elements. Please let me know your views on this.

I am implementing progressive UI disclosure pattern in my application. Using which I am disabling the next elements. So based on input of one element the next element is enabled.

But I have a problem is since the next element is disabled, the tab from the current element is taking the focus to the end of document or the tab header when tab out. As the progressive enables the element after the tab out, while this was happening the next element was not enabled so tab was lost outside the document.

So my requirement is to enable tab on the disabled elements and also on mobile/tablet devices the click events should at least be registered on the disabled elements. Please let me know your views on this.

Share Improve this question asked Oct 15, 2014 at 11:45 IndianWebDeveloperIndianWebDeveloper 1291 gold badge1 silver badge4 bronze badges 7
  • 1 Do you have an example code? – Kalimah Commented Aug 9, 2018 at 5:13
  • @mac9416 Disabled elements cannot be focused. – benvc Commented Aug 9, 2018 at 21:35
  • 1 @benv I like that. And maybe just style the button to look disabled. – crenshaw-dev Commented Aug 9, 2018 at 23:20
  • 1 @mac9416 yes, seems easier than overriding the focus behavior of disabled elements. Good luck. – benvc Commented Aug 9, 2018 at 23:22
  • 1 Please add sample code. – Akrion Commented Aug 12, 2018 at 21:14
 |  Show 2 more ments

5 Answers 5

Reset to default 6 +50

Answer

To answer the question (as we already discussed in the ments), disabled elements can't be focused.

Workaround

For those looking for a workaround that gives a visual indication that an element is "disabled" and also prevents default functionality while still preserving focusability and click events, following is a simplified example where the submit button appears to be disabled and is prevented from "submitting" unless the input contains some text (also restores "disabled" state if input is cleared).

const input = document.querySelector('input');
const button = document.querySelector('button');
input.addEventListener('input', (event) => {
  const target = event.currentTarget;
  const next = target.nextElementSibling;
  if (target.value) {
    next.classList.remove('disabled');
  } else {
    next.classList.add('disabled');
  }
});

button.addEventListener('click', (event) => {
  const target = event.currentTarget;
  if (target.classList.contains('disabled')) {
    event.preventDefault();
    console.log('not submitted');
  } else {
    console.log('submitted');
  }
});
button {
  background-color: #fff;
  color: #0d47a1;
  border: 2px solid #0d47a1;
}

button.disabled {
  background-color: #e0e0e0;
  color: #bdbdbd;
  border: 2px solid #bdbdbd;
  cursor: default;
}
    
button.disabled:focus {
  outline: none;
}
<input type="text">
<button class="disabled">Submit</button>

You could add an event listener to the keydown event and listen for the tab key like in the code snippet,

document.addEventListener("keydown", keyDown);

function keyDown(e) {
  switch (e.which) {
    case 9:
      var focused = $(document.activeElement);
      var all_inputs = $("input");
      var disabled_inputs = $("input[disabled='disabled']");
      var diff = all_inputs.length - disabled_inputs.length;
      var index = all_inputs.index(focused);
      if (index == diff - 1 && index != -1 && disabled_inputs.length > 0) {
        $(disabled_inputs[0]).removeAttr("disabled").focus();
        e.preventDefault();
      }
      break;
  }
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input type="text" placeholder="this is not disabled!"> <br>
<input type="password" placeholder="this is not either!"> <br>
<input type="button" value="This is!" disabled="disabled"> <br>
<input type="submit" value="This is too!" disabled="disabled">

This will enable the element as you tab onto it. It doesn't effect normal behavior otherwise. I am assuming you don't want to re-disable the element after the focus leaves.

It is better if we have some part of the code to understand better your problem.

For this:

I am implementing progressive UI disclosure pattern in my application. Using which I am disabling the next elements. So based on input of one element the next element is enabled.

You must first handle an event for the first element and then in the callback function you need to enable/disable the second element, let say:

For enable:

$('#firstElement).on('click', function(){ $('#secondElement').removeAttr('disabled') })

For disable:

$('#firstElement).on('click', function(){ $('#secondElement').attr('disabled', 'disabled') })

Hope this could help.

On my idea a input event listener or change event listener for dropdown and input fields work better for your case. E.g:

$(document).on('input','input',function()
{
  $(this).next().prop('disabled',false);
}

or

$(document).on('change','input',function()
{
  $(this).next().prop('disabled',false);
}

You can use tabindex attribute in this case. Please refer below code, you need to update tabindex of disabled elements in a way that they get skipped when you press tab.

as per w3schools

The tabindex attribute specifies the tab order of an element (when the "tab" button is used for navigating).

    <script src="https://ajax.googleapis./ajax/libs/jquery/3.3.1/jquery.min.js"></script>


    <input class="input-1" tabindex="1" value="1">
    <input class="input-2" tabindex="2" value="2">
    <input type="button" onclick="changeTabIndex();" value="change-tab-index">
    <input class="input-3" tabindex="3" value="3">
    <input class="input-4" tabindex="4" value="4">
    <input class="input-5" tabindex="5" value="5">
    <script type="text/javascript">
    	function changeTabIndex() {
    		$(".input-5").attr("tabindex",4);
    		$(".input-4").attr("tabindex",5);
    	}
    </script>

发布评论

评论列表(0)

  1. 暂无评论