Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
React Comment Section Character Counter

Hey fellow developers,

so, I'm having this issue with React, and although I think I'm getting to show the 2000 as a number and a value overall, when I write inside the comment section, the number changes to NaN. I tried using the parseInt() in order to turn the span into a number, but nothing happens... Any suggestions?

Here's the code :)

import React from "react";
import Header from "../Header";
import "./contact-form.css";

function characterCounter() {
  let text = document.getElementById("message").value;
  let textLength = text.length;
  let counter = document.getElementById("characterCounter");
  let counterNumber = parseInt(counter);
  counter.textContent = counterNumber - textLength;
}

class ContactPage extends React.Component {
  render() {
    return (
      <div>
        <Header />
        <h1>Contact.</h1>
        <form action="" className="contact-form" id="contactForm">
          <label>Your Name</label>
          <input type="text" placeholder="First Name..." />
          <label>Your Last Name</label>
          <input type="text" placeholder="Last Name..." />
          <label>Your Email</label>
          <input type="text" placeholder="Email..." />
          <label>Your comment</label>
          <textarea
            name=""
            id="message"
            cols="10"
            rows="10"
            placeholder="Hey..."
            onKeyDown={characterCounter}
          ></textarea>
          <span>
            Max words <span id="characterCounter">2000</span>
          </span>
          <button id="buttonSubmitContact" type="submit">
            Submit
          </button>
        </form>
      </div>
    );
  }
}

export default ContactPage;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

So... you are using React, don't you? Well I will rewrite the component to solve the issue in the React's way

const MAX_CHARS = 2000
function ContactPage() {
    const [charsLeft, setCharsLeft] = useState(0);
    const updateCharsCount = ({target:{value}}) => {
        setCharsLeft(MAX_CHARS - value)
    }
    return (
        <div>
            <Header/>
            <h1>Contact</h1>
            <form action="" className="contact-form" id="contactForm">
                <label>Your Name</label>
                <input type="text" placeholder="First Name..."/>
                <label>Your Last Name</label>
                <input type="text" placeholder="Last Name..."/>
                <label>Your Email</label>
                <input type="text" placeholder="Email..."/>
                <label>Your comment</label>
                <textarea
                    name=""
                    id="message"
                    cols="10"
                    rows="10"
                    placeholder="Hey..."
                    onChange={updateCharsCount}
                ></textarea>
                <span>Max words {charsLeft}/{MAX_CHARS}</span>
                <button id="buttonSubmitContact" type="submit">
                    Submit
                </button>
            </form>
        </div>
    );
}

export default ContactPage;

By the way, I've used functional component instead of class component, it's the current approach

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda