Developing 2 potential results from textarea input using NODE.JS and HTML

Today, I encountered an issue while working on my bot website project that responds to textarea input. When I attempted to test it with two different commands, one of the commands stopped working unexpectedly. I'm puzzled by this and would greatly appreciate any assistance in resolving it. Here is a snippet of my code:

function reply() {
  const rplyBox = document.getElementById('rplyBox')
  const input = document.getElementById('msgBox').value;
  if (input === "help") {
    const embed = document.createElement("div");
    embed.classList.add("w3-round-xxlarge", "w3-margin-top", "w3-animate-opacity", "w3-margin-bottom", "w3-margin-right", "w3-margin-left")
    const title = document.createElement("h1")
    title.appendChild(document.createTextNode("Help"));
    const desc = document.createElement("h3")
    desc.appendChild(document.createTextNode("All of my commands!"));
    const field1 = document.createElement("h6")
    field1.classList.add("w3-border", "w3-topbar", "w3-bottombar", "w3-leftbar", "w3-rightbar", "w3-round-xlarge", "w3-border-black")
    field1.appendChild(document.createTextNode("kill"));
    const br = document.createElement('br');
    field1.appendChild(br);
    field1.appendChild(document.createTextNode('{name of the person to kill (figuratively)}'));
    embed.appendChild(title);
    embed.appendChild(desc);
    embed.appendChild(field1);
    rplyBox.appendChild(embed);
  } else if (input === "kill") {
    const cmdInput = input.value.split("\n");
    const person = cmdInput[1];
    const embed = document.createElement("div");
    embed.classList.add("w3-round-xxlarge", "w3-margin-top", "w3-animate-opacity", "w3-margin-bottom", "w3-margin-right", "w3-margin-left")
    const title = document.createElement("h1")
    title.appendChild(document.createTextNode("You killed " + person + "!"));
    const desc = document.createElement("h3")
    desc.appendChild(document.createTextNode("Well, they must've deserved it."));
    embed.appendChild(title);
    embed.appendChild(desc);
    rplyBox.appendChild(embed);
  }
}
html {
  height: 100%;
  width: 100%;
}

.main {
  margin-left: 5%;
  margin-right: 5%;
}

* {
  font-family: monospace;
}

t {
  font-size: 8vw;
}


body {
  animation: blueGrad;
  animation-iteration-count: infinite;
  animation-duration: 6s;
}

.sticky {
  position: -webkit-sticky;
  position: sticky;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>replit</title>
  <link href="style.css" rel="stylesheet" type="text/css" />
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <script src="script.js"></script>
</head>
  
<body style="background-image: url('https://bot.valueyu.repl.co/bg.mp4'); background-size: cover;">
  <div class="main">
    <t>bot.valueyu</t>
    <p>bot.valueyu is a bot made by Aarav Saini/Valueyu. For commmands type "<span
        class="w3-text-white">help</span>".</p>
    <br>
    <p>Each command disappears after 5 seconds. Some commands which need time might last longer, In which case, the time will be defined in the bottom-left corner.</p>
    <hr>
    <textarea class="w3-round-large w3-bar w3-margin-top sticky" id="msgBox"></textarea>
    <br>
    <br>
    <br>
    <button id="submit" onClick="reply()" class="w3-round-xxlarge w3-button sticky">Send</button>
    <hr>
    <div class="w3-bar w3-white w3-round-large" id="rplyBox">
        
    </div>
  </div>
</body>

</html>
I would really appreciate any insights or suggestions. Thank you so much!

Answer №1

@Alex I've updated my code:

function reply() {
  const rplyBox = document.getElementById('rplyBox');
  const input = document.getElementById('msgBox').value;
  const killCmdInput = document.getElementById("msgBox").value.split(' ');
  
  if (input === "help") {
    const embed = createEmbed("Help", "All of my commands!", "kill", "{name of the person to kill (figuratively)}");
    rplyBox.appendChild(embed);
  } 
  else if (input === "kill") {
    const person = killCmdInput[1];
    const embed = createEmbed("You killed " + person + "!", "Well, they must've deserved it.");
    rplyBox.appendChild(embed);
  }
}

function createEmbed(titleText, descText, fieldText = null, fieldValue = null) {
  const embed = document.createElement("div");
  embed.classList.add("w3-round-xxlarge", "w3-margin-top", "w3-animate-opacity", "w3-margin-bottom", "w3-margin-right", "w3-margin-left");

  const title = document.createElement("h1");
  title.appendChild(document.createTextNode(titleText));

  const desc = document.createElement("h3");
  desc.appendChild(document.createTextNode(descText));

  if (fieldText && fieldValue) {
    const field = document.createElement("h6");
    field.classList.add("w3-border", "w3-topbar", "w3-bottombar", "w3-leftbar", "w3-rightbar", "w3-round-xlarge", "w3-border-black");
    field.appendChild(document.createTextNode(fieldText));
    
    const br = document.createElement('br');
    field.appendChild(br);
    
    field.appendChild(document.createTextNode(fieldValue));

    embed.appendChild(title);
    embed.appendChild(desc);
    embed.appendChild(field);
  } else {
    embed.appendChild(title);
    embed.appendChild(desc);
  }

  return embed;
}
but I'm still encountering issues. UPDATE: It's working now, thank you for your help @Alex!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to apply changes to every class in JavaScript?

Check out this HTML and CSS code sample! body{ font-family: Verdana, Geneva, sans-serif; } .box{ width: 140px; height: 140px; background-color: red; display: none; position:relative; margin-left: auto; margin-right: auto; } .bold{ font ...

The dropdown item in Tailwindcss is unexpectedly flying off the edge of the screen rather than appearing directly under the dropdown button

Currently, I am developing an application using Rails, Vue, and TailwindCss 1.0+ I am facing an issue while creating a dropdown menu for my products. When I click on the dropdown button, the items in the dropdown fly off to the edge of the screen instead ...

Looking for a smart way to extract all the selected elements from a form?

I am attempting to retrieve all the checked items from this form using JavaScript. I have looked at previous solutions, but none of them fit my requirements. <form id="checkform" class="container" style="margin-top:20px;"> <input type="checkb ...

Obtaining a response in string format using the $.ajax function

var module = (function(){ return{ loadData: function(url, success, error){ $.when($.ajax({ type: 'GET', cache: false, url: url, contentType: 'application ...

Feeling a bit lost when trying to kickstart a project in node.js and utilizing npm install

Greetings, I am currently in the process of learning and consider myself a beginner. Recently, I explored the chat tutorial from the get-started section on socket.io. As I continue to expand my knowledge by exploring other resources, one question remains ...

scrollable header within the confines of the container

I have been trying to find a solution for my scrolling issue, but I just can't seem to get it right. I want the content within a div to scroll inside its container without affecting the header. Here's the updated JSFiddle link with the jQuery cod ...

Issue with Javascript Date and Time Validation

My application includes code that is supposed to display HTML pages based on today's date and the time of day (morning, afternoon, or evening). However, it seems like there is an issue with how the time is being checked. Currently, at 2:53pm, only the ...

Ways to trigger the keyup function on a textbox for each dynamically generated form in Angular8

When dynamically generating a form, I bind the calculateBCT function to a textbox like this: <input matInput type="text" (keyup)="calculateBCT($event)" formControlName="avgBCT">, and display the result in another textbox ...

Is it time to swap out those divs for tables in your email template?

I have set up my template like this and I am questioning whether I should replace every div with a table. Let's start with the first div in the example. Do I really need to create a new table element, followed by tr and td tags just for a banner? I u ...

Using third-party libraries like jQuery, CSS, and JavaScript in your React project by directly importing them into the index.html file can be a more efficient approach compared

When working with React, is it advisable to import external JavaScript, jQuery, and CSS files into the index.html file in the public folder? Are there any potential performance implications associated with this practice? I have utilized some jQuery functi ...

How Express + Angular routing is leading to an endless loop and system crash

Currently, I am developing a Node application using Express and Angular. Angular is being used for routing, and my routes are configured as shown below: app.config(['$routeProvider', function ($routeProvider) { $routeProvider.when('/', ...

How can I design a search box similar to the one on the android.com website?

Is it possible to replicate the search box effect on the Android website, where the menu items fade out and the search box expands upon clicking the search icon?view image here See the difference before and after clicking the search icon ...

Tips for Managing Disconnection Issues in Angular 7

My goal is to display the ConnectionLost Component if the network is unavailable and the user attempts to navigate to the next page. However, if there is no network and the user does not take any action (doesn't navigate to the next page), then the c ...

Watch as data is inserted in MongoDB with the help of ExpressJS

My MongoDB collection is rapidly growing and I need to perform specific actions when new documents are inserted. How can I monitor the insertion of new models in order to trigger these actions? I have looked into older solutions like mongo-observer, but t ...

Guide on retrieving the response headers with jQuery and AJAX

Connection: keep-alive Content-Length: 2231 Content-Type: text/html; charset=utf-8 Date: Thu, 13 Sep 2018 07:37:46 GMT ETag: W/"8b7-XOXhf04O/VM7yxWQ561PEgxRfz8" x-auth: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YjlhMGEyM2Q0NmI3YjFmYTQzNWI ...

The window fails to retain the scroll position when overflow is enabled

Whenever I click on a specific div, I use jQuery to dynamically apply overflow: hidden to the html and body. $(".mydiv").on("click", function() { $("html, body").css("overflow", "hidden"); }); However, this action causes the window to scroll to the to ...

Upon clicking the IconButton within the ImageListItemBar, reveal the image within a Material-UI Dialog

import * as React from 'react'; import Box from '@mui/material/Box'; import ImageList from '@mui/material/ImageList'; import ImageListItem from '@mui/material/ImageListItem'; import ImageListItemBar from '@mui/m ...

When attempting to run the npm install mathjs command, an error is displayed

Trying to install mathjs using npm but encountering an error: npm install mathjs The error message received is as follows: npm WARN tar TAR_ENTRY_ERROR UNKNOWN: unknown error, write npm WARN tar TAR_ENTRY_ERROR UNKNOWN: unknown error, write npm WARN tar T ...

How do I use NodeJS (Express) to send a retrieved file from a REST call to the browser?

I am currently experiencing issues with the code below in a node express application that is meant to retrieve a binary file (PDF) and display it in the browser. Despite my efforts, the code does not seem to be functioning correctly! When I utilize the cod ...

How can I align rectangles with different heights to display side by side using Javascript?

Currently, I am designing a press page for a website where the headlines/articles are displayed in rectangles. To achieve this layout, I am using the following CSS: .press-blocks{ column-count: 4; column-gap: 2em; padding-left: 10%; padding ...