To cancel a line break when you Enter?

Form works on Ajax and sending data occurs when the user presses Enter

$("#txt_post").keypress(function(event){
 if(event.as the ctrlkey property && event.keyCode == 13){
 document.getElementById('txt_post').value += '\r\n';
return;
}

if(event.keyCode == 13){
send();
}
});


All according to plan. Sending appears when you press Enter. A newline is added when pressing CTRL+ENTER. But the problem is the following:

There is the following text(example):
Daily practice shows that the new model of organizational activity entails the implementation process and the modernization of systems of mass participation. The task of the organization, especially the beginning of the daily work on the formation position entails the implementation process and the modernization model of development. Ideological considerations of the highest order, and a new model of organizational activity is an interesting experiment verification forms of development


If the cursor is at the end, it's all right, but if somewhere in the text, then pressing Enter, in the place where the cursor was added a line break
Daily practice shows that the new model of organizational activity entails the implementation process and the modernization of systems of mass participation. The task of the organization, especially the beginning of the daily work on the formation positions involve process

implementation and modernization model of development. Ideological considerations of the highest order, and a new model of organizational activity is an interesting experiment verification forms of development


How to fix it?
March 19th 20 at 09:22
1 answer
March 19th 20 at 09:24
if(event.keyCode == 13){
 event.preventDefault(); 
send();
 }

I would wrote:
$("#txt_post").keypress(function(event){
 if(event.as the ctrlkey property && event.keyCode == 10){
 let self = event.target;
 let n = self.selectionStart;
 self.value = self.value.substr(0,n-1) + '\r\n' + self.value.substr(n-self.value.length);
return;
}
 else if(event.keyCode == 13){
event.preventDefault();
 send(); 
}
});
$("#txt_post").keypress(function(event){
 if(event.as the ctrlkey property && event.keyCode == 13){
 document.getElementById('txt_post').value += '\r\n';
}

 if(event.keyCode == 13){
event.preventDefault();
send();
}
 });


I tried that. But this produces a new problem, and it was not satisfied the first part is not added a newline by pressing CTRL+Enter - Lina46 commented on March 19th 20 at 09:27

Find more questions by tags jQuery