Aligning text vertically within a textarea

I have been trying to implement the following HTML and JavaScript code:

<html>
<textarea id='txt' style='height:500px; widht:400px'></textarea>
<input type='text' id='input'/>
<input type='button' id='clicMe' onkeypress='fillTxt()' />

<script>
function fillTxt(){
  if(event.keyCode == 13)
      document.getElementById('txt').value += document.getElementById('input').value + <br/>;
}
</script>
</html>

My goal is to click a button and have the text from the input field added to the textarea, vertically aligned at the bottom. In other words, the newly added text should appear at the bottom of the textarea.

For example:

.-----------------------------.
|                             |
|                             |
|                             |
|  this is some text          |
'-----------------------------'

UPDATE:

I have managed to make it work now with the following code:

<div id="tBox" style=" 
    position:absolute;
    top:400px;
    left:220px;
    width:600px;
    height:334px;
    color:#666666;
    padding:5px;
    margin-bottom:25px;">

        <div id="tHolder" style="
            width:500px; 
            height:300px; 
            background-color:transparent; 
            color:#008080; 
            font-weight:bold; 
            border-style:hidden; 
            left:5px; 
            background-color:transparent;
            position:relative;
            overflow:auto;">

            <p id="txt" style='position:absolute; bottom:0; left:0;'></p>

        </div>

        <input type="text" style="width:500px; position:absolute; bottom:15px; left:8px;" id="input" name="input" onkeydown="fillTxt()" />

</div>

Answer №1

There are actually two options available, but neither of them can be considered "natural" since we are essentially making things do something they don't typically do (but really, who doesn't enjoy a little challenge?)

The first option involves using a content editable <p> tag that is positioned at the bottom of a <div> container. This method is preferable as it provides elements that function like textboxes, allowing for selection and cursor placement anywhere:

 #contentEditableDiv{
        width:300px;
        height:200px;
        margin:100px auto;
        border: 1px solid #000;
        background:#EEE;
        position:relative;
        overflow:auto;
    }
    
    #editableP{
        background:red;
        min-height:10px;
        position:absolute;   
        bottom:0;
        left:0;
        right:0;
    }
 <div id="contentEditableDiv">
        <p id="editableP" contentEditable="true"></p>
    </div>
    
   

Alternatively, you can use a <div> as a placeholder for styling and have a hidden textarea synced with it. This method requires a bit more logic to replicate a true textbox, but here is the basic concept:

window.onload = (function(){
    
        var textArea = document.getElementById('textArea');
        var hiddenTextArea = document.getElementById('hiddenTextArea');
        var textHolder = document.getElementById('textHolder');
        
        textArea.addEventListener('click',function(){
            hiddenTextArea.focus();
        },false);
        
        hiddenTextArea.addEventListener('keyup',function(e){
            textHolder.innerHTML = hiddenTextArea.value;
        },false);
    
    
    }());
#textArea{
        width:300px;
        height:200px;
        margin:100px auto;
        border: 1px solid #000;
        background:#EEE;
        position:relative;
        overflow:auto;
    }
    #textHolder{
     position:absolute;   
        bottom:0;
        left:0;
    }
    
<div id="textArea">
        <span id="textHolder"></span>
    </div>
    <textarea id="hiddenTextArea"></textarea>​
    
    
    

Answer №2

Is it possible to insert text at the bottom of a textarea?

One approach could be to specify the number of rows in the textarea and use JavaScript to append multiple linefeeds before the input text.

Another option is to have a single-row textbox and add padding to create the illusion of empty rows above it.

Answer №3

It is beyond the capabilities of CSS to achieve this task. Using JavaScript code may be necessary in order to calculate the height of the textarea and adjust the padding accordingly.

Answer №4

One potential solution is to insert a text field within a div element and specify the distance between the top of the textarea and the top of the div, as well as the distance between the bottom of the textarea and the bottom of the div.

By automatically adjusting the height of the textarea, this approach may prove effective.

Answer №5

Hey there, I recommend adding some padding to the textarea like so:


textarea{
    width:400px;
    height:400px;
    padding-top:100px;
    -webkit-box-sizing: border-box;
   -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
     -o-box-sizing: border-box;
        box-sizing: border-box;
}

Check out the live demo here

http://jsfiddle.net/L5TJL/

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

Anticipated the presence of a corresponding <tr> in the <table> within the server HTML, but encountered hydration failure due to discrepancies between the initial UI and the server-rendered content

Next.js Version 13.2.3 In Next.js, it is advised not to use the table element. "use client"; export default function index() { return ( <table> <tr> <th>Company</th> ...

Using jQuery to enhance the functionality of the drop-down select feature

I am facing an issue with my index file code. When I select something from the drop-down menu, I expect to see a related dropdown appear. Although I have added this code to my file, I am unable to get the drop down to show after selecting the main type. ...

Obtain information stored locally when an internet connection is established

I'm currently facing an issue with my Local Storage data retrieval process in Vuejs while being online. My ToDo app setup consists of Vuejs, Laravel, and MySQL. When the internet connection is available, I store data in localStorage. The ToDo app has ...

Press the html button and activate the button using Visual Basic

Being new to Visual Basic, I have created a simple app using the WebBrowser component. The web page that loads contains an HTML button. I want this HTML button press to enable a button in Visual Basic, but I don't know how to do it. I have researched ...

Numerous items lined up in a single row

I have designed a skills section that includes a name and corresponding image. Now, I would like to pass the skill's name into a child component. My goal is to display three of these skills on the same row, but using ngFor currently results in each sk ...

What is the proper location to place the CSS I wish to implement for a module?

As I embark on creating my inaugural DotNetNuke website, I find myself faced with the task of adding an HTML module to a page. To style the text within it, I have been advised to utilize CSS classes. I'm curious about where .css files should be place ...

Tips for selecting the key as an option value in VueJS when using v-for loop

Currently, I'm utilizing Vue 3 along with Bootstrap 5. In my project, there is a select element containing various options. Right now, the displayed text (option.TEXT) is passed as the value to my methods when any changes are made. However, what I ac ...

Displaying error messages rather than "Server Error" is a better practice

After switching hosts, I've noticed a change in how errors are displayed. On my old host, syntax errors would be directly shown, indicating where the issue was located. However, on my new host, all I see is: The website encountered an error while ...

Having trouble with jest mocking a function - it's not functioning as expected

I decided to create a simple test using jest to simulate a date change function. Here is the code snippet: import React from 'react'; import '@testing-library/jest-dom'; import { render, screen } from '@testing-library/react' ...

What is the equivalent of Node's Crypto.createHmac('sha256', buffer) in a web browser environment?

Seeking to achieve "feature parity" between Node's Crypto.createHmac( 'sha256', buffer) and CryptoJS.HmacSHA256(..., secret), how can this be accomplished? I have a piece of 3rd party code that functions as seen in the method node1. My goal ...

Populate an AngularJS select dropdown with JSON data and automatically pre-select the specified value

I am currently dealing with 2 entities: project and project_types. Each project can have one or multiple project_types associated with it. Using ajax (ng-init), I am retrieving the project data (including its related project_types) and all available proje ...

Unable to successfully send a POST request from the client-side frontend to the local node server; only GET requests are functioning properly

While attempting to submit a post request to my local server at localhost:8080/page, I encountered the message 'Cannot GET /page'. My browser is Google Chrome and the network tab in the developer tools shows: Below is the front end code featurin ...

Struggling with jQuery not updating class attributes for list items, any solutions to resolve this issue?

In order to have a unified navigator for various classes, I have stored it in a separate HTML file and am attempting to import it into the home page using jQuery. <!-- ======= Header ======= --><!-- End Header --> <div id="header-par ...

Troubles with flexibility in image placement

I am working on adjusting my design to accommodate smaller screen sizes where elements may shift to a second line. I want these elements to align to the left instead of being centered. The image below illustrates my goal: https://i.sstatic.net/CzX5V.png C ...

Implementing a JavaScript function that uses the AJAX method to confirm and update

I am currently attempting to update a database using the JavaScript confirm() function in combination with AJAX. My goal is to have "accepted_order.php" run if the user clicks "OK," and "declined_order.php" run if the user clicks "Cancel," all without caus ...

Collecting the standard output and standard error of a Node-API module executed within an Electron framework

During the development of my Node-API module alongside an Electron application, I encountered a dilemma. The N-API module runs within the render process of Electron due to its intricate API that would be challenging to navigate through a context bridge. Fu ...

Tips for positioning two divs side by side in block formation

I'm attempting to display two distinct div elements as blocks, one for the name and the other for the names. Here is the Fiddle The names block should be displayed as a separate block next to the name div, regardless of screen responsiveness. How ca ...

Why isn't the function in my React child component passing its parameters to the parent component's function as expected?

In the parent: const [currentPinPosition, setCurrentPinPosition] = React.useState({ lat: 0 , lng: 0 }); const updateCurrentPinPos = (position) => { console.log(position); setCurrentPinPosition({ lat: position.lat, lng: position.lng }); }; / ...

Find the identification number by searching through the text

I'm trying to find a way to retrieve the node id during a text search. Here's an example: http://jsfiddle.net/53cvtbv9/529/ I attempted using two methods to get the id of a node after the search: console.log($('#jstree').jstree(true). ...

typescript warns that an object may be potentially null

interface IRoleAddProps { roles: Array<IRole> } interface IRoleAddState { current: IRole | null } class RoleAdd extends React.Component<IRoleAddProps, IRoleAddState> { state = { current: null, } renderNoneSelect = () ...