3

Spacebar does not work in button element when i use the HTML5 contenteditable. But its working perfect in div element. How can i get it to working? can someone help me?

Please check it here: https://jsfiddle.net/Issact/f6jc5th4/

HTML:

<div contenteditable="true">
This is editable and the spacebar does work in "Div" for the white-space
</div>
<button contenteditable="true">
This is editable and the spacebar does not work in "button" for the white-space
</button>

CSS:

div {
  background-color:black;
  padding:20px;
  color:#fff;
}
button {
  background-color:green;
  padding:20px;
  color:#fff;
}
Devbuddy
  • 231
  • 4
  • 15

5 Answers5

4

This may help you:

const button = document.querySelector('button[contenteditable]')

button.addEventListener('keydown', function (event) {
  if (event.code !== 'Space') {
    return
  }
  event.preventDefault()
  document.execCommand("insertText", false, ' ')
})

Look example here: on JSFiddle

Raserad
  • 41
  • 4
1

wrap the text inside the button tag with a span make that span contenteditable

html

<div contenteditable="true">
This is editable and the spacebar does work in "Div" for the white-space
</div>
<button >
<span contenteditable="true">
This is editable and the spacebar does not work in "button" for the white-space
</span>
</button>

css

div {
  background-color:black;
  padding:20px;
  color:#fff;
}
button {
  background-color:green;
  padding:20px;
  color:#fff;
}

hope this works

Chandra Shekhar
  • 3,550
  • 2
  • 14
  • 22
1

Use this

<button 
    contenteditable="true" 
    onclick="if (!event.x && !event.y && !event.clientX && !event.clientY) {event.preventDefault(); insertHtmlAtCursor('&nbsp;');} else { }"> This is editable and the spacebar does not work in "button" for the white-space </button>

<script type="text/javascript">
function insertHtmlAtCursor(html) {
    var range, node;
    if (window.getSelection && window.getSelection().getRangeAt) {
        range = window.getSelection().getRangeAt(0);
        node = range.createContextualFragment(html);
        range.insertNode(node);
        window.getSelection().collapseToEnd();
        window.getSelection().modify('move', 'forward', 'character');
    } else if (document.selection && document.selection.createRange) {
        document.selection.createRange().pasteHTML(html);
        document.selection.collapseToEnd();
        document.selection.modify('move', 'forward', 'character');
    }
}
</script>

Added live demo here https://jsfiddle.net/jalayoza/f6jc5th4/3/

Hope this helps

Jalay Oza
  • 772
  • 7
  • 11
0

You'll want to make an event listener that prevents the default event when the spacebar is clicked and then use execCommands to put the enter in programatically.

MDN Link: https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand

Niels
  • 529
  • 2
  • 6
  • 20
0

@Jalay's answer almost worked but not quite. This is what worked for me:

Comment out window.getSelection().collapseToEnd(); part of Jalay's function:

function insertHtmlAtCursor(html) {
    var range, node;
    if (window.getSelection && window.getSelection().getRangeAt) {
        range = window.getSelection().getRangeAt(0);
        node = range.createContextualFragment(html);
        range.insertNode(node);
        //window.getSelection().collapseToEnd();
        window.getSelection().modify('move', 'forward', 'character');
    } else if (document.selection && document.selection.createRange) {
        document.selection.createRange().pasteHTML(html);
        document.selection.collapseToEnd();
        document.selection.modify('move', 'forward', 'character');
    }
}

Then listen for the spacebar keyup event, and insert a space as needed:

$(document).on('keyup', 'button', function(e){
   if(e.keyCode == 32){
       insertHtmlAtCursor(' ');
   }
});

This also has the benefit of working for dynamically added content.

enter image description here

Cybernetic
  • 12,628
  • 16
  • 93
  • 132