The problem is that .keypress()
does not fire when the backspace is used. You should use the input
event instead. Also, Math.max
greatly simplifies your remaining character calculation.
If you want to enforce an actual cap on the number of characters in your <textarea>
, you could always add the attribute maxlength="300"
.
$('textarea').on('input', function() { $('#remainingC').text( Math.max(0, 300 - this.value.length) + ' / 300' ) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea maxlength="300"></textarea> <span id='remainingC'></span>
Related Posts:
- Failed to load resource: the server responded with a status of 404 (Not Found)
- Maximum call stack size exceeded error
- Can I use complex HTML with Twitter Bootstrap’s Tooltip?
- Google Chrome Uncaught (in promise) DOMException while playing AUDIO
- How to make a word underline in Markdown
- How do I check whether a checkbox is checked in jQuery?
- (Google Map API) Geocode was not successful for the following reason: REQUEST_DENIED
- jQuery.click() vs onClick
- Check if checkbox is checked with jQuery
- Add swipe left/right to web page, but use default swipe up/down
- How can I get the data-id attribute?
- Check if checkbox is checked with jQuery
- How to resolve the C:\fakepath?
- Angular: mat-form-field must contain a MatFormFieldControl
- Adding options to select with javascript
- How does the data-toggle attribute work? (What’s its API?)
- JavaScript get element by name
- Use images like checkboxes
- How to read xml file contents in jQuery and display in html elements?
- Completely disable scrolling of webpage
- How do I scroll to an element using JavaScript?
- Programmatically change the src of an img tag
- display variable image using cookies
- Open URL in same window and in same tab
- Create table with jQuery – append
- How to convert selected HTML to Json?
- How would I call a javascript function in html?
- How to Open New window on every Click
- “.addEventListener is not a function” why does this error occur?
- Cannot read property ‘getContext’ of null, using canvas
- Full-screen iframe with a height of 100%
- How to move an element after another element using JS or jquery?
- How to use Javascript to read local text file and read line by line?
- Parse an HTML string with JS
- Why is the jquery script not working?
- Shortest way to print current year in a website
- DOMException: Failed to load because no supported source was found
- Textarea Auto height
- Partial render in HTML/JavaScript
- Is it possible to append to innerHTML without destroying descendants’ event listeners?
- How to add onload event to a div element
- Print the contents of a DIV
- How to create a HTML Cancel button that redirects to a URL
- Showing an image from an array of images – Javascript
- Embedding instagram webpage inside an iframe
- Set value to currency in
- HTML Entity Decode
- why angularjs ng-repeat not working
- Scroll to bottom of div?
- How can I copy to clipboard in HTML5 without using flash?
- HTML5 rich-text inside textarea
- How to scroll an HTML page to a given anchor
- How do I check if a cookie exists?
- Highlight a word with jQuery
- How to change the buttons text using javascript
- how to call a onclick function in tag?
- How do I refresh a DIV content?
- how to display a div triggered by onclick event
- JavaScript button onclick not working
- jQuery sort elements using data id
- HTML make text clickable without making it a hyperlink
- getContext is not a function
- JavaScript code to stop form submission
- Getting HTML form values
- How to change the text of a label?
- Change an image with onclick()
- JavaScript – Count Number of Visitor for Website
- Strip HTML from Text JavaScript
- How to run a function when the page is loaded?
- Disabled href tag
- Creating Accordion Table with Bootstrap
- Creating Multiple Modals on a Single Page
- Closing Bootstrap modal onclick
- Fire oninput event with jQuery
- Changing the selected option of an HTML Select element
- window.location() not working, not opening page
- How to clear the canvas for redrawing
- Default text on input
- img onclick call to JavaScript function
- How to scroll to top of page with JavaScript/jQuery?
- Calling a JavaScript function in another js file
- How to embed an autoplaying YouTube video in an iframe?
- JavaScript code not running in HTML5 document
- Cannot read property ‘style’ of undefined — Uncaught Type Error
- Javascript, viewing [object HTMLInputElement]
- Unexpected token < in first line of HTML
- Why onbeforeunload event is not firing
- How to make HTML element resizable using pure Javascript?
- How to set “style=display:none;” using jQuery’s attr method?
- WordPress Bootstrap Handburger Menu Wont Open
- Contact Form 7 – Give each checkbox a class?
- How to display post content in the block editor
- Do I just put the html in a page when enqueueng or do I also have to reference js file from the html page [closed]
- Make a script work within a page
- Putting custom html/js page into Elementor as it’s own block
- Add Paypal button redirect to a specific page after completing
- Can’t Listen to KeyDown in TInyMCE Iframe (jQuery) and Pass it to Parent HTML FORM
- how to do open a link in a open window from menu bar? [closed]
- Making the HTML list to a checkbox tree with the plugin jstree [closed]
- Sending GravityForms to custom HTML