Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
I have this class that I want to make dynamic and make it work with multiple elements

So I got this class to truncate text but it's fixed, I want to to make more dynamic. I want to pass the length of text I want to truncate and make it work with multiple elements. I'm a newbie in JS classes so it's a but confusing for me right now. thank you in advance.

class readMore {
  constructor() {
    const end = 300;
    this.content = '.readmore__content';
    this.buttonToggle = '.readmore__toggle';
  }
  initiate() {
    this.setNodes();
    this.init();
    this.addEventListeners();
  }
  setNodes() {
    this.nodes = {
      contentToggle: document.querySelector(this.content),
    };
    this.buttonToggle = this.nodes.contentToggle.parentElement.querySelector(this.buttonToggle);
  }
  init() {
    const { contentToggle } = this.nodes;
    this.stateContent = contentToggle.innerHTML;
    contentToggle.innerHTML = `${this.stateContent.substring(200, `${end}`)}...`;
  }
  addEventListeners() {
    this.buttonToggle.addEventListener('click', this.onClick.bind(this));
  }
  onClick(event) {
    const targetEvent = event.currentTarget;
    const { contentToggle } = this.nodes;

    if (targetEvent.getAttribute('aria-checked') === 'true') {
      targetEvent.setAttribute('aria-checked', 'false');
      contentToggle.innerHTML = this.stateContent;
      this.buttonToggle.innerHTML = 'Show less';
    } else {
      targetEvent.setAttribute('aria-checked', 'true');
      contentToggle.innerHTML = `${this.stateContent.substring(200, `${end}`)}...`;
      this.buttonToggle.innerHTML = 'Show more';
    }
  }
}
const initReadMore = new readMore();
initReadMore.initiate();
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda