What is the best way to showcase user input in a text field?

I want to input some text into a field and have it immediately displayed in a textbox.

It seems simple enough. All I need is an input field for the text, a button to submit it, and a textbox to show the entered text. However, my code doesn't seem to be working as expected.

This is the code I am using:

<!DOCTYPE html>
<link rel="stylesheet" href="style.css">

<script type="text/javascript">

function displayText(){
  var userInput = document.getElementById("userInput");
  var displayArea = document.getElementById("displayArea");
  var newText = userInput.value;
  displayArea.innerHTML = newText;
}

</script>
<div class="Webview">

  <div class="message_container" id="myForm" ></div>

  <form class="send_container">
    <input type="text" id="userInput">
    <button type="submit"
    value="Send message"
    onclick="displayText()">

  </form>


</div>


@charset "UTF-8";

.Webview{
  height: 400px;
  width: 300px;
}

.message_container{
  height: 80%;
  width: 100%;
  border:5px solid green;
}

.send_container{
  height: 20;
  width: 100%;
}

.send_container input{
  width: 70%;
  height:20%
  border:2px solid #1CE615;
}

.send_container button{
  width: 30%;
  height:20%;
}

Answer №1

It appears there may be some confusion surrounding the usage of ids and naming conventions in your code. It seems like you are trying to access elements with incorrect names. Here is a potential solution:

<input id="textField" type="text">
  <p>
    <input type="button" id="theButton" value="click me!" 
      onclick="document.getElementById('div').innerHTML = 
      document.getElementById('textField').value" />
  </p>
  <h3><div id="div"></div></h3>

Answer №2

After reviewing your code, I have identified a few issues that need to be addressed:

  1. The input element used for user input should have an id attribute in order to be accessible with getElementById. You can achieve this by adding an id attribute to the <input type="text"> tag.
  2. To target an element accurately, make sure to provide a proper id or class. For instance, you can use the message_container class and access it using
    document.getElementsByClassName('message_container')[0]
    . Then, update the content of the element using the innerHTML property.

Based on the explanation provided, here is a suggested solution:

const output = () => {
  const inputText = document.getElementById("text");
  const messageContainer = document.getElementsByClassName('message_container')[0];
  messageContainer.innerHTML = inputText.value;
}
<div class="Webview">
  <div class="message_container" id="myForm"></div>
  <form class="send_container">
    <input id="text" type="text" />
    <button type="button" onclick="output()">Send Message</button>
  </form>
</div>

This solution should address the issues mentioned above. Feel free to try it out and let me know if you need further assistance!

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

Tips for using regular expressions to extract body content

I have this code saved in a variable. <html> <head> . . anything . . </head> <body anything=""> content </body> </html> or <html> <head& ...

Updating dropdown selection with ng-model in AngularJS

nameList includes [“Julia”, “Evan”, “Tomas”]; select ng-model=“names” ng-options=“x for x in nameList” In my controller, I make a service api call to GetNameByID/{id}” and based on the id, I need to set the default value of the drop ...

Is there a way to make a link open only on the second click, instead of the first?

Is it possible to have a popunder advert that opens only when the page is clicked a second time, not the first time? Here's the code for the popunder: <script type="text/javascript"> var uid = '35190'; var wid = '65325'; &l ...

Utilize ng-repeat to display a series of images, with the first image being placed within a unique div

My challenge lies in displaying product images using ng-repeat, where one image is located in a separate div. Let me share my code and explain what is happening. The API response provides product details and images [{"id_product":"1000","name":"Nikolaus ...

Angular : How can a single item be transferred from an array list to another service using Angular services?

How to Transfer a Single List Item to the Cart? I'm working on an Angular web application and I need help with transferring a single item from one service to another service and also displaying it in a different component. While I have successfully i ...

Access to Web Share API requires permission

I am currently attempting to integrate the Web Share API feature into my testing web application, but unfortunately, I seem to be encountering some difficulties. Below is the code snippet I have been working with: const newVariable: any = navigator; {newV ...

Is it possible to display a Processing message at the beginning of a datatables table already containing data?

Within my Laravel 5.7 application, I have implemented the "yajra/laravel-datatables-oracle": "~8.0" library and found a helpful thread on customizing the processing message at this link. I adjusted the processing message styling as follows: .dataTables_pr ...

The icons in webviews on mobile apps (iOS and Android) fail to render correctly

My font icons are behaving inconsistently on webkit-based mobile browsers when used on mobile devices. When hovering over the span on a desktop browser, the icon properly fills its container: https://i.sstatic.net/qzrmz.png However, when hovering over t ...

Place a div according to the location of the mouse click

Is there a way to create a popup in Angular 4 that appears next to the mouse click coordinates? I want it to work similar to how events are created in Google Calendar. ...

Transforming the hue of a radio-button

When it comes to the default CSS code for radio buttons, they appear gray when unselected and blue when selected: https://i.stack.imgur.com/hsazr.png However, I have a specific requirement for them to be black in both states. In order to achieve this, I ...

Having trouble with popovers after upgrading to bootstrap v4

I am facing an issue while trying to migrate Bootstrap from version 3 to 4. The problem specifically lies with popovers and the popper.js library. Each time I hover over an element, the following error is displayed: Uncaught TypeError: Cannot read property ...

Python Indexing of Iframes

Can an iframe be indexed in a different manner? The following code is what I currently have: driver.switch_to_frame(driver.find_element_by_tag_name("iframe[2]")) However, this solution does not seem to be effective. ...

The concept of a basic JavaScript 'while loop' completely eludes me

Currently delving into the world of JavaScript over at Code Academy, I find myself at the tail end of the while loops section. Check out this code snippet: j = 0; while (j < 3) { j++; } When executed, the above code displays a 2 on the console a ...

Tips for enhancing the contents of a single card within a react-bootstrap accordion

Currently, I am facing an issue with my columns expanding all cards at once when utilizing react-bootstrap accordion. My goal is to have each card expand individually upon clicking on its respective link. However, I am encountering difficulties in implem ...

Increase the height of the div to align with the height of the other div

Is there a way to use CSS to automatically adjust the height of both the right and left <div> elements so they fill the background-color based on the greater height, without setting a specific height for either? The left <div> can vary in heig ...

What is the method to invoke a function within a factory in angularjs by using a string parameter?

I have a complex logic that I want to encapsulate in an AngularJS factory for easy use with dependency injection. The challenge is that the logic is dynamic, so I don't know in advance what functions will be available. What I have is a string represen ...

Provide a numerical representation of how frequently one object value is found within another object value

Account Object Example in the Accounts Array: const accounts = [ { id: "5f446f2ecfaf0310387c9603", picture: "https://api.adorable.io/avatars/75/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6b7d7a666 ...

Angular: "btn" class vanishes when the button is toggled

I am facing an issue with the button's class change functionality. I am using the [ngClass] directive to switch between Bootstrap classes for the button style. However, when I click the button, the "btn" class seems to disappear from the code. Instead ...

A guide on transferring data between two arrays of objects using TypeScript

I am working on implementing a get request within a component that retrieves a response: getPaymentIntents(): Observable<Payment>{ const url: string = 'https://store.com//payments'; return this.http.get<Payment>(url); } ...

Passing the Angular $scope property into a function as an argument results in it being overwritten

I'm facing a peculiar issue that I can't quite wrap my head around. In my Angular 1.4 project, I have a variable called $scope.videoModel on the scope object, which serves as a model for capturing form data. When the form is submitted, the model ...