Создайте div и стилизуйте его с помощью -webkit-Appearance и -moz-Appearance и установите атрибут contenteditable
- Также создайте скрытый элемент ввода
- С помощью javascript добавьте текст элемента div в скрытый элемент
- Затем вы можете использовать его в форме для отправки
Скрытый вводбудет сериализован как данные формы, однако свойство css не поддерживается в IE 6-10 и 11.
Запустите фрагмент и протестируйте его:
const input = document.getElementById("input"),
button = document.getElementById("send-button"),
message = document.getElementById("message");
input.addEventListener('keyup', function (e) {
const target = e.target;
var value = target.textContent || target.innerText;
//Escape any space before and after input
value = value.replace(/(\s+)/g, '');
//Set the value to message input
message.value = value;
})
//Send the message input
button.addEventListener('click', function (e) {
e.preventDefault();
//assume your sending the input to server
// if the value is not empty
if(message.value !== '') {
alert(message.value)
}
});
#input {
-moz-appearance: textfield;
-webkit-appearance: textfield;
display: inline-block;
background-color: white;
background-color: -moz-field;
border: 1px solid #ccc;
/* font: -moz-field;
font: -webkit-small-control; */
font-size: .9rem;
margin-top: 5px;
padding: .8rem .5rem;
width: 200px;
}
<div id="input" contenteditable data-placeholder="Enter your text"></div>
<input type="hidden" id="message"/>
<button type="submit" id="send-button">
Send
</button>
Живой пример в JSfiddle