Position the text so that it is aligned below and to the right of the text box

I'm currently working on a project in ASP.NET that involves a resizable textbox with a character limit display positioned below and to the right of the box. The challenge I am facing is ensuring this text remains properly aligned as the size of the textbox changes based on different browser window sizes and monitor resolutions.

Despite my attempts to align the text to the right, I have not been able to achieve the desired result. How can I ensure that the character limit text always appears below and to the right of the resizable textbox, even when the box is being resized?

Bootstrap is utilized for div alignment in this particular project.

--- UPDATE ---

Below are some code examples along with the scripts typically used:

// EXAMPLE 1 
<div class="col-md-3 mb-3 ml-2">
     @Html.EditorFor(m=> m.Title, new { htmlAttributes = new { @class = "form-control characterlimit", @maxlength = 128 } })
     <div class="text-muted col-md-11 mb-3 ml-4 position-absolute" style="font-size: 12px; text-align:right">128 Character Limit</div>
</div>

// EXAMPLE 2
<div class="col-md-6 mb-3 ml-2">
     @Html.TextAreaFor(m => m.Description, new { @class = "form-control characterlimit", @rows = 3, @maxlength = 2500 })
     <div class="text-muted col-md-3 mb-3 ml-0 position-absolute" style="font-size: 12px; text-align:right">2500 Character Limit</div>
</div>

<script>
$(".characterlimit").keyup(function () {
     var box = $(this);
     var max = box.attr("maxlength")
     var len = box.val().length;
     box.next().text((max - len) + " Characters Left");
});
</script>

Answer №1

Typically, it is recommended to enclose both the text box and character limit indicator in a container and then position the character limit indicator to the right within that container.

const updateLimit = input => {
  input.nextElementSibling.innerHTML = (input.maxLength - input.value.length) + ' characters remaining';
}
document.querySelectorAll('textarea').forEach(input => {
  updateLimit(input)
  input.addEventListener('keypress', evt => {
    updateLimit(evt.target)
  })
})
body, textarea {
  font-family: sans-serif;
  font-size: 16px;
}

body {
  background: #eee;
}

.s1 {
  margin: 0 auto;
  background: white;
  margin-bottom: 2em;
  display: inline-block;
  max-width: 100%;
}

textarea {
  width: 600px;
  height: 5em;
  padding: 5px;
  box-sizing: border-box;
  max-width: 100%;
}

p {
  margin: 0;
  font-size: 10px;
  text-align: right;
}
<span class="s1">
  <textarea maxlength="50">Lorem ipsum dolor sit amet</textarea>
  <p class="count"></p>
</span>

<br>

<span class="s1">
  <textarea maxlength="100">Etiam ullamcorper velit vitae sem pulvinar, dignissim efficitur nulla interdum </textarea>
  <p class="count"></p>
</span>

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

Using HTML and JavaScript to create a link that appears as a URL but actually directs to a JavaScript function

I am working on an HTML page and I am trying to create a link that appears to go to 'example.html' but actually goes to 'javascript:ajaxLoad(example.html);'. Here is what I have tried: <a href="example" onclick="javascipt:ajaxLoad( ...

Set up a trigger to activate when a WordPress User Role is selected, or alternatively, execute JavaScript

Looking for a solution to detect when a new user is created with a custom User Role set to lessee. Options include adding a trigger through WordPress filters or actions, or detecting the change in JavaScript using an event listener. I have identified the ...

What is the best way to create rounded thumbnails with the paperclip gem?

I noticed that the latest version of Basecamp is implementing this feature, you can check it out in this link. I am curious about how to replicate this in my Rails 3 application. ...

Have you ever wondered how the automatic video play on scroll feature works on Tiktok.com and YouTube shorts when using a mobile device?

My goal with React JS is to develop a website similar to Tiktok, where the video below will automatically play with sound as the user scrolls down. I attempted to set this up using window.addEventListener("scroll",...), but after looking into it further, ...

The dropdown login form is malfunctioning on my Wordpress website

After reading various tutorials and guides online, I managed to set up a login screen. You can view the code in my jsfiddle: Jsfiddle. Unfortunately, I am facing issues with making the code function correctly. Being new to Jquery, I might have made a begin ...

Enhancing an Image Gallery using CSS

Currently in the process of building a website for an upcoming event, and naturally, I need to create an event calendar. For inspiration, I have been referencing this example for its gallery layout and hover effects. However, I am hoping to customize thi ...

Guide to uploading a PDF to Google Drive and embedding it on an HTML static website

I manage a static HTML site for a small food shop. They require monthly menu uploads in PDF format. I believe uploading to Google Drive would be a more efficient solution than creating a separate admin view for file uploads. Can anyone advise me on how to ...

What could be causing my AJAX script to not execute upon form submission?

My form's ajax functionality is not working properly. When I try to submit the form, nothing happens. Can someone please assist me with this issue? $( '#my-form' ) .submit( function( e ) { $.ajax( { url: 'u_mainslid ...

What is the best way to position one of my links at the end of a bootstrap navbar?

insert image description here I'm currently working on implementing a Bootstrap navbar and I am trying to align one of the links, specifically the last one, to the right side. Although I have searched through the documentation, I have not been able t ...

Persistent banner designed to accompany blog posts

I've been attempting to insert a banner ad that runs along the left side of my blog post. Unfortunately, all my efforts so far have caused it to display above the content, pushing the blog post down the page. You can view the live link here. Here i ...

The Bootstrap grid exhibits instability at specific breakpoints

I have been working on a blog app that utilizes the bootstrap grid system. It functions perfectly on small, medium, and large screens. Here are the images: https://i.sstatic.net/AgEDe.png https://i.sstatic.net/Ydwfn.png https://i.sstatic.net/Q9Fup.png ...

What could be causing my video not to display a thumbnail on mobile devices until it is played?

When I view my website on mobile devices, there is no background image displayed before playing a video. Below is the HTML code I am using: <div class="row " > <div class="col-12 text-center"> <video control ...

Tips for accessing form data that has been automatically uploaded in PHP

I've been trying to automatically post data using JavaScript and retrieve it in a PHP file. However, I am facing an issue where the PHP file is not able to retrieve the data. I have set up an automatic form that takes input from another form and send ...

Is there a way to prevent the window.status from appearing?

I currently have the following code snippet: <a class="button accessLink" id="loginLink" href="#" data-action="Login" data-dialog="access" data-disabled="false" data-entity="n/a" ...

Top method for centering a flexible SVG vertically once the page width becomes too narrow

Currently, I have two SVG images displayed side by side on a webpage. One SVG needs to maintain a fixed size while the other should scale as needed, and I have achieved this functionality. However, I am facing an issue where I want the two SVGs to align v ...

completing data tables and presenting them to clients

I am currently working on a website for a Nutrition specialist who requires a monthly diet regimen table with five meals daily. The doctor will be responsible for completing these tables, which will then be presented to clients. It's important that th ...

Having trouble retrieving my .scss bundle file from a different directory

I am trying to access my .scss file that imports all the other .scss files (they are all imported in styles.scss). Even though I have added the path to my angular cli file, the file is still not accessible and no styling is being applied to my html elemen ...

How can we eliminate all elements from jQuery except for the first and second elements?

HTML <div class="geo_select"> <h3>header 3</h3> in Above HTML code i want to remove all element except <h3> and default content<div> inside the <div class='geo_select'> in jquery.. How to remove all ...

Issue occurred while attempting to delete a row from the AJAX enabled GridView

I'm facing an issue where I can successfully edit data inside my gridview, but when I try to delete a record it throws a server error. The specific error message that is displayed during an AJAX postback on a delete operation is: 208|error|500|A pro ...

Here, it is possible for the padding-right value to be equal to zero

I am relatively new to CSS and I am currently in the process of testing and experimenting to better understand how it works. For instance, in the code snippet provided, we see that the selector .box h3 has a padding: 6px 10px 4px 10px;. Therefore, the pad ...