I have a chat application and I want users can send message just by pressing Enter key. for now, they send message by mouse clicking.
here is my codes :
var chat_input_send = document.createElement('button')
chat_input_send.setAttribute('id', 'chat_input_send')
chat_input_send.setAttribute('disabled', true)
chat_input_send.innerHTML = `<i class="far fa-paper-plane"></i>`
var chat_input = document.createElement('input')
chat_input.setAttribute('id', 'chat_input')
chat_input.setAttribute('maxlength', 1000)
chat_input.placeholder = `${parent.get_name()}. Say something...`
chat_input.onkeyup = function(){
if(chat_input.value.length > 0){
chat_input_send.removeAttribute('disabled')
chat_input_send.classList.add('enabled')
chat_input_send.onclick = function(){
chat_input_send.setAttribute('disabled', true)
chat_input_send.classList.remove('enabled')
if(chat_input.value.length <= 0){
return
}
parent.create_load('chat_content_container')
parent.send_message(chat_input.value)
chat_input.value = ''
chat_input.focus()
}
}else{
chat_input_send.classList.remove('enabled')
}
}
I know this is the way but I cant make it work in my codes:
anyone can help me?
$('#idoftextbox').keypress(function (e) {
var code = e.keyCode || e.which;
if (code === 13) {
//enter has been pressed
};
});