0

I'm using the bootbox.js prompt as the following :

document.getElementById('picture').addEventListener('click', function(){
    bootbox.prompt('URL de l\'image: ', function(result){
        document.execCommand('insertImage', false, result);
    });
}, false);

But this line document.execCommand('insertImage', false, result); seems that it doesn't work.

I tried to do an alert instead and it worked.

Why the document.execCommand() doesn't executed ? and how can I solve this problem ?

Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191

1 Answers1

0

Now, this is just more than I can fit in a comment.

The element that is set as contenteditable="true" has likely lost its focus ( or it's contenteditable = true is not set )

Try

document.getElementById('picture').addEventListener('click', function(){

    var editabletarget = document.getElementById("currentcontenteditableid");
    bootbox.prompt('URL de l\'image: ', function(result){
        /* reset editable and focus() */
        editabletarget.setAttribute("contentEditable", true);
        editabletarget.focus();

        document.execCommand('insertImage', false, result);
    });
}, false);

Replacing currentcontenteditableid with your current contenteditable element id.

Give that a try, hope works ( seems likely ). If not please give us

  • a little more info on what bootbox.js is and what it is doing
  • some of the html
Rob Sedgwick
  • 5,216
  • 4
  • 20
  • 36
  • Thanks it worked, but still there is a problem that the image is inserted in the start of the editable div. – Renaud is Not Bill Gates Feb 28 '14 at 23:26
  • I know! welcome to the fun world of contenteditable `cursor` recall! The answer to that is quite large, so best searching on it or posting a new question. Here is a good start - http://stackoverflow.com/questions/1181700/set-cursor-position-on-contenteditable-div. The priciple is to store the cursor position before the click and then reset it after the focus before inserting the image. – Rob Sedgwick Feb 28 '14 at 23:30