Adjusting the character limit for my textarea input

Here is the styling for my textarea:

textarea {
    min-width: 120px;
    min-height: 90px;
    padding: 8px 10px;
    background: #FFF;
    border: 1px solid #E0E0E0;
    border-radius: 2px;
    outline: none;
    padding: 8px 10px;
    box-shadow: inset 0 1px 2px rgba(#E0E0E0, .50), 0 -1px 1px #FFF, 0 1px 0 #FFF;

    @include transition(all .2s ease-in-out);

    @include placeholder {
        font-family: 'Titillium Web', sans-serif;
    }
}
textarea:focus {
    border: 1px solid lighten(#3498db, 10);

    @include box-shadow(0px, 0px, 6px, rgba(lighten(#3498db, 10), .5), false);
}
.textarea-group {
    position: absolute;
    min-width: 120px;
    min-height: 90px;

    textarea[limit] {
        display: block;
        resize: none;
        border-bottom: none;
        border-bottom-left-radius: 0; border-bottom-right-radius: 0;
        margin: 0;
        position: relative;
        width: 100%;
    }
    .chars {
        display: block;
        margin: 0;
        position: relative;
        width: 100%;
        padding: 8px 10px;
        background: #FAFAFA;
        color: #999;
        font-size: 11px;
        border: 1px solid #E0E0E0;
        border-bottom-left-radius: 2px; border-bottom-right-radius: 2px;
    }
}

I have a script to detect changes in my code, but it's not updating my counter. Can someone help me figure out why? I suspect it might be related to how I've selected the elements.

$(function() {
    $('.textarea-group > textarea[limit]').keyup(function() {
        var max = $(this).attr('limit');
        var length = $(this).val().length;
        length = max - length;
        $(this + ' > .chars span').text(length);
    });
});

The script is being called successfully.

N.B. I want this to apply individually to all elements with the same name :)

Thanks!

EDIT Here's the final jquery code:

$(function() {
    $('.textarea-group > textarea[limit]').keyup(function() {
        var max = parseInt($(this).attr('limit'));
        var length = $(this).val().length;
        length = max - length;
        $('.chars span', $(this).parent()).html(length);
    });
});

Credits to Francisco (answer) and Akshay (integer parse)

Answer №1

this isn't considered a string, so it might not work as expected:

$(this + ' > .chars span')

Also, this should refer to the textarea, which means you need to go up one level in the DOM.

Instead, consider using the second parameter context:

$('.chars span', this.parentNode)

Another option is to use parent and find:

$(this).parent().find('.chars 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

ngClass with multiple conditions

I am currently working on implementing the following functionality - I have two pre-set classes that are combined with some component variables successfully. However, I now need to include an additional conditional class. Although the first part is functi ...

The video controls cannot be seen

I'm encountering an issue while trying to incorporate controls into a full-width HTML5 video. Despite following this example: https://jsfiddle.net/nfouvpe5/, the controls remain invisible. Below is the code snippet I am using: <div class="overlay" ...

Is there a way to change HTML retrieved from an AJAX request before inserting it into the document?

I am utilizing an ajax call to retrieve an HTML page from the server; the ajax call is structured as follows: function loadHtml() { $.ajax({ type : 'GET', async : false, url : &apos ...

Issue with activating a Modal through a button inside a table row on React

I'm currently working on two files: Modal.js and Users.js. Users.js features a table with an API get query linked to it, and in the last column of the table, there's a dropdown for each row that contains three buttons: View, Edit, and Delete. My ...

Is it possible to create dynamic meta tags in Angular that will appear in a Twitter-style card preview?

My project involves building a dynamic website using a Java app that serves up a REST-ish JSON API along with an Angular9 front end. A key requirement is the ability to share specific URLs from the app on platforms like Twitter and Slack, which support Twi ...

The issue with the AngularJS component layout engine is that the fxLayoutAlign attribute set to "stretch" is not

It seems that when using the angular/flex-layout along with angular/material2, the fxLayoutAlign="stretch" directive is not functioning correctly. However, the directives fxLayoutAlign="start" and fxLayoutAlign="end" are working as expected. You can see th ...

What is the best way to halt my jQuery animation?

(function ($) { $(document).ready(function () { $('#road').pan({ fps: 30, speed: 9 }); $('#city2').pan({ fps: 30, speed: 2 }); $('#city3').pan({ fps: 30, speed: 5 }); $('#sky') ...

What are the benefits of incorporating 'i in array' into my personalized 'array.indexOf' function?

I've come across code like this multiple times: function customArrayIndexOf(item, array){ for (var i = 0, l = array.length; i < l; i++) { if (i in array && array[i] === item) return i; } return -1; } But I'm unsur ...

Scripts in iframes within webviews are not preloading and executing properly

When using the <webview> tag in the renderer process within the <body> of a web page, the following code is used: <webview src="http://somewebpage.com" preload="somescript.js"> The script somescript.js will be execute ...

Is it possible to add items to a JS object that isn't an array?

Here is an example of the object I am working with: { "_id": "DEADBEEF", "_rev": "2-FEEDME", "name": "Jimmy Strawson", "link": "placeholder.txt", "entries": { "Foo": 0 } } To access this data in my JavaScript code, I use a $.getJSON call. ...

The ng-isolate-scope is not properly connected to the specified templateUrl

I am encountering difficulties when trying to implement isolated scope with templateUrl. Here is my directive test: beforeEach(ngModule('app.directives')); var scope, compile beforeEach(inject(function($rootScope, $compile){ scope = $ro ...

What could be causing the Bootstrap navbar to not toggle when the button is clicked?

Attempting to create a website featuring a collapsible navbar, but encountering some issues. The button is properly identified with the correct ID assigned. Jquery and bootstrap have been included in the necessary order at the bottom of the body. However, ...

Selecting a single checkbox automatically selects all other checkboxes

Is there a way to automatically check all other checkboxes if one checkbox is checked? Here's the code snippet I have: <tr> <td class="small"><asp:CheckBox ID="chkReportModuleName" runat="server" name="report"></asp:CheckBox& ...

Discovering modifications to CSS using selenium

Currently, I am working on an ajax form where CSS elements are dynamically changed. I am curious to know if it is feasible to verify these changes using Selenium. For example, checking if the background color changes from #ffffff to #000000 after clicking ...

Ways to deselect checkboxes with Typescript and Jquery

I have a set of checkboxes all labeled with the same 'types' class. There is a button on my webpage that should be able to toggle between selecting and deselecting all the checkboxes in this group. When checking the boxes, I use the following sc ...

Toggling reverses multiple CSS keyframe animations

I've been working on a unique animation to switch my website between dark and light mode. The animation is quite complex, so I anticipate that the solution will be equally intricate. Currently, I am toggling between classes, but this approach makes i ...

Transform your .obj files into .glb files effortlessly with npm and Laravel

I've hit a roadblock for 2 days now and can't seem to find a solution. Could someone lend me a hand with this? Query: What's the best way to convert a .obj file to .glb? I attempted using obj2gltf npm package but integrating it with Larav ...

Discovering targeted information from object utilizing React

When using the fetch method to retrieve film data, how can I extract specific details from the returned object? I am able to access and manipulate properties like name, genre, cast, trailers, and recommendations, but I'm struggling with retrieving the ...

Display or Conceal a DIV depending on a PHP jQuery variable

Is there a way for me to Hide/Show a DIV based on a Variable? Here is what I currently have. The variable is posted into an INPUT <input type="text" id="showhidediv" name="showhidediv" value="<?php echo $ta; ?>"> This is the jQuery code I am ...

Get the current executing event in jQuery by detecting multiple jQuery events

When I have a series of jQuery events like this: $(selector).on('click keydown blur', function(){ //do something } Is there a way to determine which event triggered the function at the current time? For instance, consider the following sce ...