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">
?
Asked
Active
Viewed 3,206 times
3

Hashem Qolami
- 97,268
- 26
- 150
- 164

Banana Code
- 759
- 1
- 12
- 28
-
give some height and width to the div, may be? – L4reds May 15 '15 at 08:16
-
So you want a div that looks like an input type=text ? – Steven Lemmens May 15 '15 at 08:21
-
Yeah, a single-line-and-plain-text contenteditable div. It looks like a “”, but not. – Banana Code May 15 '15 at 08:28
-
Try:
1 Answers
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
-
-
@RedWhale It can be done by replacing the content of the element with the text nodes. In plain JavaScript, there's `element.textContent` that can get/set the text content of an element. The jQuery version is `.text()`. – Hashem Qolami May 15 '15 at 09:15
-
the content is not scrollable - see http://stackoverflow.com/questions/16670931/hide-scroll-bar-but-still-being-able-to-scroll – 4esn0k Apr 24 '16 at 06:25
-
-
-
@4esn0k True, but the goal was to present the form of a text input which does not have a scrollbar ui naturally. – Hashem Qolami Apr 24 '16 at 08:19
-
2the is still scrollable when you are trying to select some text or when doing "drag and drop" – 4esn0k Apr 24 '16 at 09:22
-
@4esn0k Oh, I got the point. That's right. I didn't think of it at the time of writing. – Hashem Qolami Apr 24 '16 at 10:24