5

How can I make the softkeyboard come up with the bootstrap modal when auto focusing a field? It sounds easy, but I am unable to do so as of yet.

The focus part works but not the keyboard.

I am trying to save the user a tap.

I can leverage 'shown.bs.modal' and set the focus but the softkeyboard will not show up automatically. The user still needs to retap the field. How can I force the softkeyboard to come up.

The code I am currently playing with (pretty much):

        this.$container.on('shown.bs.modal', function () {
            console.log('shown.bs.modal');
            setTimeout(function () {
                var $ctrl = $(jqselector);
                $ctrl.addClass('active').focus();
            }, 500);
        });
        this.$container.modal({
            backdrop: (this.config.showModal ? 'static' : true)
        })
        .on('hidden.bs.modal', function () {
            $(this).remove();
        });

SE question related to just focus

another question

Edit: After looking at the bootstrap code a bit, it looks like it ads focus to the modal control after all of the processing. I assumed something like this was happening which is why I added the setTimeout, but even with a large delay, no luck. I will look at the bootsrap code a little more closely this weekend


Bounty edit: Bootstrap code:

  Modal.prototype.show = function (_relatedTarget) {
    var that = this
    var e    = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })

    this.$element.trigger(e)

    if (this.isShown || e.isDefaultPrevented()) return

    this.isShown = true

    this.checkScrollbar()
    this.$body.addClass('modal-open')

    this.setScrollbar()
    this.escape()

    this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))

    this.backdrop(function () {
      var transition = $.support.transition && that.$element.hasClass('fade')

      if (!that.$element.parent().length) {
        that.$element.appendTo(that.$body) // don't move modals dom position
      }

      that.$element
        .show()
        .scrollTop(0)

      if (transition) {
        that.$element[0].offsetWidth // force reflow
      }

      that.$element
        .addClass('in')
        .attr('aria-hidden', false)

      that.enforceFocus()

      var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })

      transition ?
        that.$element.find('.modal-dialog') // wait for modal to slide in
          .one('bsTransitionEnd', function () {
            that.$element.trigger('focus').trigger(e)
          })
          .emulateTransitionEnd(300) :
        that.$element.trigger('focus').trigger(e)
    })
  }

  Modal.prototype.enforceFocus = function () {
    $(document)
      .off('focusin.bs.modal') // guard against infinite focus loop
      .on('focusin.bs.modal', $.proxy(function (e) {
        if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
          this.$element.trigger('focus')
        }
      }, this))
  }

I have been playing with code in a timout on the show and shown custom modal events. The code pretty much looks like the following.

            setTimeout(function (e) {
                $(':focus').trigger('blur'); 
                $(document).off('focusin.bs.modal');
                var $ctrl = $(jqSelect);
                $ctrl.trigger('focus');
                $ctrl.trigger('click');
            }, 750);
Community
  • 1
  • 1
jumpdart
  • 1,702
  • 16
  • 35
  • Try `$(':focus').trigger('blur'); $ctrl.addClass('active').click().focus();`. If it works I'll explain, but I'm not sure it will work in a custom event. – dave Aug 21 '14 at 21:35
  • I have some higher priority things to work on today/this weekend but hopefully a little bounty will get someone to help solve my problem. I will try out any suggestions probably this coming monday – jumpdart Sep 05 '14 at 14:04
  • The problem you are having may not be related to Bootstrap modals at all. Do you have working Javascript that shows the soft keyboard without the modal (i.e., an input on the page that receives focus once the page has loaded)? – Andy W Sep 12 '14 at 15:31
  • @AndyW Yes... for example.. if you place a click event on a button within the modal html that executes the js above, the soft keyboard pops up without an issue – jumpdart Sep 15 '14 at 21:22
  • 3
    @jumpdart: But that requires user interaction (click), yes? See this answer: http://stackoverflow.com/a/15133808/1303740 – Andy W Sep 17 '14 at 00:34
  • @AndyW I am pretty much doing that. the focus code is fine and would work if the modal already existed but I am having trouble focusing immediately when the modal is generated. or the focus works, but the soft keyboard does not show. This is a different issue than the question you linked – jumpdart Sep 17 '14 at 13:50
  • you can duplicate this pretty easy by having a input as content for a bootstrap modal and trying to focus with softkeyboard as soon as the modal shows without any further user interaction. – jumpdart Sep 17 '14 at 13:52
  • @jumpdart: Can you provide code (jsfiddle?) WITHOUT the modal that shows the soft keyboard without user interaction? – Andy W Sep 18 '14 at 14:12
  • the focus click combo you see in any of the linked questions does the trick but does not work with the modal, even with a timeout. Or i should say that it doesnt work when the modal is first created – jumpdart Sep 18 '14 at 16:23

1 Answers1

1

I think that this doesn't have to do with Bootstrap, but with the limitations of auto-focusing form fields on mobile devices. For example, Mobile Safari will only let you programmatically focus() an element if it is executed synchronously inside a click event handler (see this SO question for more information).

If you really want the keyboard to automatically show up, perhaps what will work is if you bind to the click/touchend event instead of bs.modal.show

var self = this;
$('.some-btn-that-triggers-the-modal').on('click', function() {
  $(jqSelector).addClass('active').focus();
});
Community
  • 1
  • 1
Travis Kaufman
  • 2,867
  • 22
  • 23
  • 2
    As an aside, we tried this auto-focusing when we were building our Mobile web app and found that most users preferred to perform the extra tap. Focusing on form fields does all kinds of crazy things on iOS like zoom in the browser, shift the viewport, etc. and it can jar the user. Just a thought :) – Travis Kaufman Oct 09 '14 at 01:59