3

I am trying to make a “single-line” and “plain-text” contenteditable div. In other words, I just want to make a <input type="text">, but not present in this form. How can I use the form of contenteditable div to present the form of <input type="text">?

Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
Banana Code
  • 759
  • 1
  • 12
  • 28

1 Answers1

6

You could disable Enter key for the contentEditable element using JavaScript. Here is a jQuery version:

$('[contenteditable]').keypress(function (e) {
  if (e.keyCode == 10 || e.keyCode == 13) {
    e.preventDefault();
    // Submit the form, etc.
  }
})
.on('paste keyup', function () {
  var _this = this;
  setTimeout(function () {
    // remove the markups
    $(_this).html($(_this).text());
  }, 0);
});
[contenteditable] * {
  display: none; /* hide all descendants */
}

[contenteditable] {
  display: inline-block;
  width: 200px;
  overflow: hidden;

  -webkit-appearance: textfield;
  -moz-appearance: textfield;
  appearance: textfield;

  white-space: nowrap;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div contenteditable>
    blah blah blah...
</div>
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164