Incorporate JavaScript to enable the transfer of text from one textarea to another upon clicking a button, while simultaneously clearing the original textarea

After working on the provided code, I have managed to create a functionality where text from one textarea is copied to another textarea when a button is clicked using JavaScript.

<head>
<script type="text/javascript">

function displayOut(){
var input=document.getElementById("txt").value;
var text2=document.getElementById("txt1");
text2.value=input;
if(input.length===0)
{
alert("Please enter a valid input");
return;
}
function eraseText() {
    document.getElementById("txt").value = "";
}
}
</script>
<body>
<h1 id="result">Javascript Exm</h1>
<textarea id="txt1" rows="10" cols="100" readonly="readonly" ></textarea>
<textarea id="txt" rows="4" cols="50"  onclick="eraseText()"></textarea>
  <input type="button" onclick="displayOut()" value="click">
</body>

However, there are modifications that I still need:

  1. When the button is clicked, the text should be copied to the second textarea and the original text in the first textarea should be cleared. I tried using an erase function but it did not work as expected.

  2. I also want the copied text to be displayed in the second textarea in a continuous format, one below the other, upon clicking the button.

Answer №1

Here is a helpful tip:

function clearText() {
    document.getElementById("txt").value = "";
}

function showOutput() {
    var input = document.getElementById("txt").value;
    var text2 = document.getElementById("txt1");
    text2.value = input;
    if (input.length === 0) {
        alert("Please enter a valid input");
        return;
    }
    clearText();

}

Demo: http://jsfiddle.net/GCu2D/840/

If you move clearText() outside of showOutput and then call it inside showOutput, it will improve the code readability.

Answer №2

Give this a try, it seems you made a small mistake - your eraseText() function is outside of the displayOut() function. Make sure to call the eraseText() function after copying the text into the second textarea.

function displayOut() {
  var input = document.getElementById("txt").value;
  var text2 = document.getElementById("txt1");
  text2.value = input;
  if (input.length === 0) {
    alert("Please enter a valid input");
    return;
  }
  eraseText(); //call the function to erase text in the textarea.

}

function eraseText() {
  document.getElementById("txt").value = "";
}
<h1 id="result">JavaScript Example</h1>

<textarea id="txt1" rows="10" cols="100" readonly="readonly"></textarea>
<textarea id="txt" rows="4" cols="50" onclick="eraseText()"></textarea>
<input type="button" onclick="displayOut()" value="click">

Answer №3

function showOutput(){
    var userInput=document.getElementById("text-input").value;

    if(userInput.length===0)
    {
        alert("Please provide valid input");
        return;
    }else{
       var outputText=document.getElementById("output-text");
       outputText.value=userInput;
       clearInput();
    }
}
function clearInput()
{
    document.getElementById("text-input").value = "";
}

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

Automate the installation of an npm package while including the --save-dev flag

There is a lesser-known trick where npm can be utilized as a Node.js module to execute commands within the code. I am looking to gather user input on which packages are needed, install them programmatically while also saving them to the package with the - ...

Having trouble with setting up the next image configuration for graphcms' images

I've been using graphcms solely for my image assets and trying to integrate them into my Next JS (v12.0.1) frontend. However, I keep getting the error message hostname not configured in next.config.js, even though I have already specified it in my nex ...

Having trouble importing Bootstrap into Next.js? It seems like the issue may be related to the

I am currently facing an issue with importing bootstrap 5.3.2 (not react-bootstrap) into my NextJS 14.1.0 project that utilizes the new App Router. My goal is to strategically utilize individual Bootstrap components (not through data-attrs). I managed to ...

Transferring information between two tables in a MongoDb database

My current database in mongo is named "sell" and it contains two tables: "Car" and "Order". In the "Car" table, there is an attribute called "price". When I run the following command in the mongo shell: db.Order.aggregate([ { $lookup: { from: ...

Difficulty altering link hover background color

I'm having trouble changing the hover effect background-color on the tweets of this page: Despite my efforts, all the links except for the latest tweets are working perfectly. What am I missing? Here's what I've been trying... <script& ...

Having trouble accessing the iframe element in an Angular controller through a directive

My webpage contains an iframe with a frequently changing ng-src attribute. I need to execute a function in my controller each time the iframe's src changes, but only after the iframe is fully loaded. Additionally, I require the iframe DOM element to b ...

What is the best method for sending variables to the `script.` block in Pug?

I am encountering an issue with the code in my index.pug file doctype html html head title= title body script(src=`${source}`) script. for (var event of events){ VClient.Event.subscribe(event, createDiv); } This is how ...

Is it possible for a conditional type in TypeScript to be based on its own value?

Is it possible to use this type in TypeScript? type Person = { who: string; } type Person = Person.who === "me" ? Person & Me : Person; ...

Complete alignment of several elements in a horizontal scrolling container

In the table-like container, I have elements with horizontal scrolling. Each element has the same width but potentially different heights. I want to add a button in the top-right corner of each element without it disappearing when scrolling. I came across ...

Display the input value in AngularJS in a customized format without changing the format in the model

When receiving an integer indicating a duration in minutes, I want to display it as hours or days if it exceeds a certain value. The ng-model should still keep the value in minutes so that any user changes are reflected accurately. For example: Reading & ...

What is the best way to differentiate the behavior of two identical classes using a single JavaScript function?

Can someone help me figure out how to make two circles move differently with the same JavaScript function? I'm new to JavaScript and struggling to differentiate between classes in HTML to achieve this. My goal is to have each circle move randomly and ...

Styling components using classes in Material-UI

I recently started using material-ui and noticed that it applies inline styles to each component. After running a test with multiple instances of the same component, I realized that there was no CSS-based styling - only repeated inline styles were generate ...

Parcel js is encountering difficulties running the same project on Ubuntu

I have encountered an issue with my JavaScript project when trying to run it on Ubuntu using parcel 2 bundler. The code works perfectly fine on Windows, but in Ubuntu, I am facing errors. Despite trying various solutions like cleaning the cache and reinsta ...

Troubleshooting Bootstrap: Navigation bar toggling causes JS functions to malfunction

My JS function is no longer working when the responsive website's breakpoint of 768px is activated (specifically, the nav var toggle/collapse). This is causing the problem where the JS function stops working. The HTML code in question is: <div cl ...

What is the best method for translating object key names into clearer and easier to understand labels?

My backend server is sending back data in this format: { firstName: "Joe", lastName: "Smith", phoneNum: "212-222-2222" } I'm looking to display this information in the frontend (using Angular 2+) with *ngFor, but I want to customize the key ...

Angular2 material dropdown menu

Currently, I am studying angular2 with its material design. One of the modules I am using is md-select for material and here is a snippet of my code: <md-select> <md-option value="1">1</md-option> <md-option value="2">2< ...

Adding multiple elements with varying content to a separate division

I am attempting to combine two div elements into a single div, but I'm encountering difficulties. Despite thoroughly examining my code, everything appears to be correct. Here's what I've tried so far. To assist me in achieving this, I am ut ...

Angular 2 navbar malfunctioning

I'm currently working on creating a navigation bar with images that, when clicked, navigate to specific components. Here is the code snippet I have so far: <nav class="sidenav col-md-1"> <ul class="menu" routerLinkActive="active"> ...

Maintaining leading zeros in Firefox and SafariMaintaining leading zeroes in Firefox and

Having trouble with the number field in Firefox and Safari browsers (and possibly Opera, although I haven't tested it there). When entering an initial value in the number field that contains leading zeros, Firefox and Safari automatically remove them. ...

What is the best way to display a placeholder instead of the state's value when a page loads?

I'm having trouble displaying the placeholder of an input instead of its state value. When the page loads, it shows an empty select box because the testType state is null initially. How can I make sure that only the placeholder is shown instead of the ...