CKEditor Link input not working in modal
Asked Answered
T

5

4

I've got a project in which I use a modal with a form and a ckeditor and the Link input doesn't work.

Here's a fiddle that recreates this problem:

http://jsfiddle.net/8t882a2s/3/

And the code of this example.

HTML:

        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                        <h4 class="modal-title" contenteditable="true" id="myModalLabel">Modal title</h4>
                    </div>
                    <div id="bodyModal" contenteditable="true" class="modal-body">
                        ...
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary">Save changes</button>
                    </div>
                </div>
            </div>
        </div>

 <button type="button" class="btn btn-default navbar-btn  margin-right-button-nav" data-toggle="modal" data-target="#myModal"><span class="glyphicon glyphicon-new-window"></span> Edit Modal</button>

JS:

CKEDITOR.disableAutoInline = true;

$(document).ready(function() {
    $('#myModal').on('shown.bs.modal', function () {
        CKEDITOR.inline('myModalLabel');
        CKEDITOR.inline('bodyModal');
    })    
});

This isn't exactly my code but the bug is the same. If you click in the modal and then try do add a Link you can't write the url in the input field.

Thanks :)

Theophrastus answered 13/12, 2016 at 12:50 Comment(0)
M
14

Despite various other approaches, this one worked for me (using Bootstrap 4):

$.fn.modal.Constructor.prototype._enforceFocus = function() {
    var $modalElement = this.$element;
    $(document).on('focusin.modal',function(e) {
        if ($modalElement.length > 0 && $modalElement[0] !== e.target
            && !$modalElement.has(e.target).length
            && $(e.target).parentsUntil('*[role="dialog"]').length === 0) {
            $modalElement.focus();
        }
    });
};

For Bootstrap 3, just override $.fn.modal.Constructor.prototype.enforceFocus instead.

Marko answered 29/3, 2017 at 21:41 Comment(5)
This should be accepted as the answer... worked for me ( bootstrap 3 ) ...Thanks !Experienced
I agree, this should be the accepted answer. This fixed the same problem that I was having with two separate HTML editors and their own dialog modals inside a Bootstrap modal!Doloritas
For me, the empty function intialisation did the trick : $.fn.modal.Constructor.prototype._enforceFocus = function() {}Ardellardella
@Marko thanks buddy, this works for me in bootstrap 4.Isabeau
Agreed - Works in BS 4.5Swizzle
C
7

To solve this issue is enough to do the following things:

  1. Add custom css rules on your body tag

    body { --ck-z-default: 100; --ck-z-modal: calc( var(--ck-z-default) + 999 ); }

  2. Set your modal options focus value to false

    • if you are using javascript to launch modal you should do it like this:

    $( '#basicModal' ).modal( { focus: false } );

    • if you are using data attributes to launch modal you should have it like this <div class="modal fade " data-focus="false" id="basicModal">
Confinement answered 18/10, 2019 at 8:25 Comment(3)
In my case, the pop up window of link form was hiding behind the modal, and your css brought it back. Thank youArdellardella
In Bootstrap 5, all I needed to do is add data-bs-focus="false" to the html. The css part was not needed.Hyperextension
Please suggest a solution for react modal. Links and Table is not workingMaypole
L
3

For me, removing tabindex="-1" from the modal attribute solves the issue.

Lafontaine answered 7/1, 2022 at 13:10 Comment(0)
D
3

If you are using bootstrap5, the following works:

Just Remove: tabindex="-1" on Bootstrap Modal

And Add: data-bs-focus="false"

Disenthrall answered 8/4 at 22:58 Comment(1)
Removing tabindex="-1" is not necessary (at least in 5.3.3), just data-bs-focus="false" does the job. However you can no longer close the modal with ESCDerosier
F
1

That's right! For me data-focus="false" solved all issues

Floris answered 22/4, 2020 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.