admin管理员组

文章数量:1323157

I am working on a chat project, and have mostly finished everything that I needed. My chat box is a textarea and for the most part it works, until I wanted to implement changing the color of certain words in the chatbox by using regex.

But looking at how I have this set up:

function writeMessageToChatBox(message, from, serverMessage=false, direct=false){
        let chat_box = $('#chatbox');
        let val = chat_box.val();
        if(!serverMessage){
            if(direct){
                console.log(replay);
                if(replay){
                    chat_box.val(val + '[Whisper to: ' + tempRecepient + ' ] ' + from + ": " + message + "\n" );
                    replay = false;
                    tempRecepient = undefined
                }
                else{
                    chat_box.val(val + '[Whisper from: ' + from + ' ] ' + from + ": " + message + "\n" );
                }
            }
            else{
                chat_box.val(val + from + ": " + message + "\n");
            }
        }
        else{
            chat_box.val(val + message + "\n");
        }
        chat_box.scrollTop(document.getElementById("chatbox").scrollHeight);

I've e to realize that textareas hold text within them in their value, but the text are not elements within the textarea so I cannot pick and choose which text gets style. From some research I saw that what I'm trying to do is not possible with a textarea. What would be another option, I assume a div container that can hold text elements?

I am working on a chat project, and have mostly finished everything that I needed. My chat box is a textarea and for the most part it works, until I wanted to implement changing the color of certain words in the chatbox by using regex.

But looking at how I have this set up:

function writeMessageToChatBox(message, from, serverMessage=false, direct=false){
        let chat_box = $('#chatbox');
        let val = chat_box.val();
        if(!serverMessage){
            if(direct){
                console.log(replay);
                if(replay){
                    chat_box.val(val + '[Whisper to: ' + tempRecepient + ' ] ' + from + ": " + message + "\n" );
                    replay = false;
                    tempRecepient = undefined
                }
                else{
                    chat_box.val(val + '[Whisper from: ' + from + ' ] ' + from + ": " + message + "\n" );
                }
            }
            else{
                chat_box.val(val + from + ": " + message + "\n");
            }
        }
        else{
            chat_box.val(val + message + "\n");
        }
        chat_box.scrollTop(document.getElementById("chatbox").scrollHeight);

I've e to realize that textareas hold text within them in their value, but the text are not elements within the textarea so I cannot pick and choose which text gets style. From some research I saw that what I'm trying to do is not possible with a textarea. What would be another option, I assume a div container that can hold text elements?

Share asked Feb 7, 2019 at 3:17 JudeJude 4512 gold badges7 silver badges17 bronze badges 7
  • 1 you can use div with contenteditable – ellipsis Commented Feb 7, 2019 at 3:19
  • You have 3 consecutive else... how's that working out for you? – zer00ne Commented Feb 7, 2019 at 3:36
  • @zer00ne They've been working fine for the most part, everything I wanted the chat client to do works. It's just I can't really give the text style products and looking around the div contenteditable route seems even more rough. – Jude Commented Feb 7, 2019 at 3:39
  • @Jude contenteditable is easier (not by much) with execCommand(). I'd reconsider using all of those elses it looks like a switch() with no breaks -- a fall through. – zer00ne Commented Feb 7, 2019 at 3:43
  • @Jude if receiving a whisper what condition must happen for it to happen? Is there anything that should happen for this to happen: val + from + ": " + message + "\n" or this: val + message + "\n"? The chat_box.scrollTop(document.getElementById("chatbox").scrollHeight); are those jQuery properties or JavaScript? I ask these questions because I'm almost done with an answer. – zer00ne Commented Feb 7, 2019 at 4:00
 |  Show 2 more ments

2 Answers 2

Reset to default 4

Use, <div> with contenteditable attribute.

.textarea{
  width:200px;
  height:50px;
  border:1px solid black;
}
<div class='textarea' contenteditable>

</div>

contenteditable Attribute

Refactored the function but I had to guess on some parameters. Used Template Literals which are Strings on steroids -- they should be your best friend dealing with all that text. The method html() is used extensively so markup can be typed or inserted in as a string.

Demo

function writeMessage(message, from = '', reply = '', serverMessage = false, direct = false) {
  let tempRx = '';
  let chat = $('.chatbox');
  let val = chat.text();
  if (!serverMessage) {
    if (direct) {
      console.log(reply);
      if (reply) {
        chat.html(`${val} <mark>[Whisper to: ${tempRx} ]</mark> ${from}: ${message}<br>`);
        reply = false;
        tempRx = undefined;
      } else {
        chat.html(`${val} <mark>[Whisper from: ${from} ]</mark> ${from}: ${message}<br>`);
      }
    } else {
      chat.html(`${val} ${from}: ${message}<br>`);
    }
  } else {
    chat.html(`${val} ${message}<br>`);
  }
  chat.scrollTop(chat[0].scrollHeight);
}

writeMessage(`Whispering, whisper test, mumble test, <b style='color:red'>belch test</b>, 

本文标签: javascriptAlternative to TextArea with more control over textStack Overflow