0

I have a bunch of textbox elements which the readonly attribute gets added to and removed from based on a bunch of different conditions.

Users have complained that pressing the Tab key gets them 'stuck' on a textbox that is readonly.

I want to now add tabindex='-1' whenever I set readonly='readonly', and then remove the tabindex attribute whenever I remove the readonly attribute.

Is there a global event that I can use to do this? I'm trying to avoid finding every instance where it occurs and doing it manually.

6
  • 2
    Events are not fired when attributes are added or removed. You should manually call a function to add tabindex when you set readonly.. Commented Jun 24, 2016 at 14:04
  • I wondered. I should have said function instead of event. But you get what I meant :) Commented Jun 24, 2016 at 14:05
  • 1
    why don't you change that attr at the same time are changing the readonly? Commented Jun 24, 2016 at 14:07
  • $('input').attr('readonly').each(function(e){$(e).attr('tabindex','-1')}); Commented Jun 24, 2016 at 14:09
  • @DaniP, that's what I was doing. It occurs about 50 times. I could have finished by now instead of taking time to ask this question but I was just curious if there was some elegant solution. Commented Jun 24, 2016 at 14:13

1 Answer 1

1

Instead of modifying tabindex each time you set/unset readonly on an element, you might use a different strategy:

  • Bind all involved elements to a focus handler
  • Then, if the element is readonly, merely trigger a tab strike

Something like this (to refine depending on your context):

$('.involved-element').focus(function() {
  if (!$(':enabled', this)) {
    $(document).trigger({type:'keydown', which:9});
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.