Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing keyboard on iPad in div contenteditable

Is there a way force the keyboard on iPad to close on blur of div 'contenteditable'??

Here is a basic jsfiddle: http://jsfiddle.net/j_tufte/7HDmN/

I'd like to have the keyboard close when a user clicks on the button.

Any thoughts super appreciated.

Thanks.

like image 907
tuddy Avatar asked Feb 22 '23 18:02

tuddy


1 Answers

As you have mentioned in your comment, element.blur() unfortunately doesn't work on an editable div. But you could instead move the focus to an actual input field and remove it again right away:

$('#otherBox').on('click', function(){
    $('#orInput').focus().blur();
});

(This uses your jsFiddle HTML code).

There are downsides to this approach: you need another input field (which you can't set to display: hidden or visibility: hidden, but you can set it's size to 0 and opacity: 0). Also, the view may scroll to the location of this input field when the above handler is invoked. So you will need to place the second input field right next or behind to the editable div.

You will also need to take care of the input field not being targeted by the previous/next buttons: set it disabled.

<input id="orInput" disabled="disabled" style="width:0; height:0; opacity:0" type="text" />

For focussing/blurring you will then need to enable the field:

$('#otherBox').on('click', function(){
    $('#orInput').removeAttr("disabled")
                 .focus().blur().attr("disabled", "disabled");
});

However, this is definitely a workaround. I haven't found any other solution yet (e.g. removing the contenteditable attribute doesn't work) but I'd very much like to hear other ideas.

like image 123
Julian D. Avatar answered Feb 24 '23 08:02

Julian D.