Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
Character countdown isn't working when value is present

I have an input field and I added a character countdown to it via vanilla Javascript. The user can add a maximum of 60 characters and the counter shows how many characters remaining while he is typing.

When the input field is empty, it shows that 60 characters are remaining. However when there is a value in the field already (when you are not typing), it shows that 60 characters are remaining. So it's not counting the number of characters of the value. It only takes it of from the 60 when I click in the input field and start typing.

If there is a value in the field, how can I make sure its characters are deducted from the max character length automatically?

Here is my code:

const POST_TITLE = document.getElementById("post_title");
const MAX_LENGTH = 60;
const CHARS = document.getElementById("chars");

function countCharacters() {
    let enteredChars = POST_TITLE.value.length;
    let charsLeft = MAX_LENGTH - enteredChars;

    if(charsLeft > 35) {
        CHARS.textContent = charsLeft + " characters remaining. Please add more characters for better SEO.";
        CHARS.style.color = "red";
    } else if(charsLeft > 10) {
        CHARS.textContent = charsLeft + " characters remaining. Almost there! Just add a few more characters to have a great SEO title.";
        CHARS.style.color = "orange";
    } else {
        CHARS.textContent = charsLeft + " characters remaining. Awesome! Between 50-60 characters is a great SEO title.";
        CHARS.style.color = "green";
    }

    if(POST_TITLE.value.length == '') {
        CHARS.style.color = "black";
        CHARS.textContent = charsLeft + " characters remaining. Please add a meta title.";
    }
}

POST_TITLE.addEventListener("keyup", countCharacters);
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  
  <form action="#" class="mt-4">
    <div class="mb-3">
      <label for="post_title" class="form-label">Post title</label>
      <input type="text" class="form-control" id="post_title" minlength="5" maxlength="60" value="some value">
      <span id="chars">60 characters remaining. Please add a meta title.</span>
    </div>
  </form>
</div>

So here I already added a value to the input field so you can see that when the page loads it's not counting the characters of the value. I've tried different events instead of the keyup, like input but didn't work.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As mentioned in the comment section, simply calling the countCharacters(); function under it's scope, will trigger the event that you seek for.

Note that the white spaces here do count as the characters.

const POST_TITLE = document.getElementById("post_title");
const MAX_LENGTH = 60;
const CHARS = document.getElementById("chars");

function countCharacters() {
    let enteredChars = POST_TITLE.value.length;
    let charsLeft = MAX_LENGTH - enteredChars;

    if(charsLeft > 35) {
        CHARS.textContent = charsLeft + " characters remaining. Please add more characters for better SEO.";
        CHARS.style.color = "red";
    } else if(charsLeft > 10) {
        CHARS.textContent = charsLeft + " characters remaining. Almost there! Just add a few more characters to have a great SEO title.";
        CHARS.style.color = "orange";
    } else {
        CHARS.textContent = charsLeft + " characters remaining. Awesome! Between 50-60 characters is a great SEO title.";
        CHARS.style.color = "green";
    }

    if(POST_TITLE.value.length == '') {
        CHARS.style.color = "black";
        CHARS.textContent = charsLeft + " characters remaining. Please add a meta title.";
    }
}
countCharacters();
POST_TITLE.addEventListener("keyup", countCharacters);
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  
  <form action="#" class="mt-4">
    <div class="mb-3">
      <label for="post_title" class="form-label">Post title</label>
      <input type="text" class="form-control" id="post_title" minlength="5" maxlength="60" value="some value">
      <span id="chars">60 characters remaining. Please add a meta title.</span>
    </div>
  </form>
</div>

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!