-6

While working on a chatting application, I've encountered the following problem: For the user to write his messages, I have implemented a textarea through HTML, and when said user presses enter, it does indeed send the message, but it also adds a newline to said textarea. Can I add something to the end of my EventListener in order to remove the newline? Thanks. (Edit: I need to be able to send the messages with ENTER, but not have the effect of it on my textarea text). Here's my EventListener:

document.addEventListener('keydown', e => {
    if (e.key == "Enter"){
        send();
        //solution goes here...
    }
});

Setting the value of the textarea to "" doing TA.value = ""; didn't work.

  • 4
    Please go read [ask]. Telling us "I've tried everything!" is not helpful. And instead of giving us an ever so vague verbal description of what you tried, you are supposed to show code. – CBroe Feb 21 '23 at 13:22
  • In addition to CBroe's comment, we need to know how the message is sent, what type of handler, you possibly also made a small mistake somewhere, so add the handler code to the question. Once we know where, a simple `trim()` should do the work – Kaddath Feb 21 '23 at 13:25
  • @CBroe I have now edited the post a tad bit and think it is a reasonable question, with the correct formatting. – Emmanuel Giouvanopoulos Feb 21 '23 at 13:51
  • 1
    It's still a duplicate of the question linked, but for your specific case I'll give you the details: you should add the `e.preventDefault()` before you call `send()` for the new line not to be added. For your info your code `TA.value = "";` didn't work because of another problem: you can't use `value` to set the content of a textarea (even if it works to retreive the content) [see details here](https://stackoverflow.com/questions/64531596/how-to-set-the-textarea-value-in-javascript) – Kaddath Feb 21 '23 at 14:09
  • @Kaddath Thank you! I can see that mine and the other question's answers are the same, yet I thought the questions themselves were different. Knowing two things have the same answer is useful, but they are not the same thing. Thanks for the explanation of the {get; set;} thing too! Although I have been a member of the SO community for a little while I haven't really been using it, since 1) I don't code that much and 2) I'm not good at coding by any means so... I can't answer questions either. That's why I am not quite familiar with the correct way to ask questions and always accept ranting! – Emmanuel Giouvanopoulos Feb 24 '23 at 12:10
  • Glad I could help you, though I saw afterwads from the link that the getter/setter is no more relevant now, it should work with modern browsers. For the duplicate it's a good thing, users often see it as an offense but for search engines it's great, because differently phrased questions will point to the same answer, which is easier to maintain, update etc – Kaddath Feb 24 '23 at 15:27

1 Answers1

-1

Event.preventDefault()

document.querySelector('textarea').addEventListener('keydown', e => {
  if (e.key === 'Enter') {
    console.log('send')
    e.preventDefault()
  }
})
<textarea></textarea>
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • 3
    @EmmanuelGiouvanopoulos we were not really ranting, but we repeat these comments to ask for clarification and details over and over, so with time we end up skipping the unnecessary parts and it may sound a bit cold ;) some of us even have these typical comments saved in a text file so we just need to copy paste :D – Kaddath Feb 21 '23 at 14:14
  • Oh... I understand! Ranting wasn't the right word anyways... Thank you very much for the time you spent typing this! – Emmanuel Giouvanopoulos Feb 24 '23 at 12:12