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

206
Visualizações
How can I get first letter of each word in Jquery?

I have multiple Names & I want to get first letter or each word. How can I do this?

My Code:-

$('.testimonial-box').each(function(){
    let clientNameFirstLetter = $(this).find('.client-name').text();
    let clientNameSecondLetter = $(this).find('.client-name').text();
    let clientNameBothLetters = clientNameFirstLetter.charAt(0) + clientNameSecondLetter.charAt(0);
    $(this).find('.img-circle').text(clientNameBothLetters);
  });
.testimonial-sectiioin{ display:flex; gap:45px;}
.testimonial-sectiioin .testimonial-box .img-circle {
  width: 100px;
  height: 100px;
  background: #ccc;
  border-radius: 50%;
  font-size: 55px;
  font-family: proximaNovaBold;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<section class="testimonial-sectiioin">
<div class="testimonial-box">
<div class="img-circle"></div>
 <p class="client-name">Rohit Verma</p>
</div>

<div class="testimonial-box">
<div class="img-circle"></div>
 <p class="client-name">Vipin Kumar Verma</p>
</div>

<div class="testimonial-box">
<div class="img-circle"></div>
 <p class="client-name">Neha Aggarwal</p>
</div>
</section>

ThankYou!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

$('.testimonial-box').each(function(){
    // Get client name
    let clientName = $(this).find('.client-name').text();
    
    // Create an array with words inside the name
    let splittedName = clientName.split(' ');
    
    // Now let's loop all words and save the first letter
    var name = ''
    splittedName.forEach(n => {
      name += n[0]
    })
    
    // Set name to current image circle
    $(this).find('.img-circle').text(name);

  });
.testimonial-sectiioin{ display:flex; gap:45px;}
.testimonial-sectiioin .testimonial-box .img-circle {
  width: 100px;
  height: 100px;
  background: #ccc;
  border-radius: 50%;
  font-size: 55px;
  font-family: proximaNovaBold;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<section class="testimonial-sectiioin">
<div class="testimonial-box">
<div class="img-circle"></div>
 <p class="client-name">Rohit Verma</p>
</div>

<div class="testimonial-box">
<div class="img-circle"></div>
 <p class="client-name">Vipin Kumar Verma</p>
</div>

<div class="testimonial-box">
<div class="img-circle"></div>
 <p class="client-name">Neha Aggarwal</p>
</div>
</section>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use split to split the text based on the space. So you will get your first name and second name.

So you can take the first letters of both words. Easy.

If you have more than 2 words in the name, you can use a map/forEach to get each letter's.

$('.testimonial-box').each(function(){
    let clientName = $(this).find('.client-name').text();
    let clientNameBothLetters = clientName.split(" ")[0][0]
 + clientName.split(" ")[clientName.split(" ").length - 1][0];
    $(this).find('.img-circle').text(clientNameBothLetters);
  });
.testimonial-sectiioin{ display:flex; gap:45px;}
.testimonial-sectiioin .testimonial-box .img-circle {
  width: 100px;
  height: 100px;
  background: #ccc;
  border-radius: 50%;
  font-size: 55px;
  font-family: proximaNovaBold;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<section class="testimonial-sectiioin">
<div class="testimonial-box">
<div class="img-circle"></div>
 <p class="client-name">Rohit Verma</p>
</div>

<div class="testimonial-box">
<div class="img-circle"></div>
 <p class="client-name">Vipin Kumar Verma</p>
</div>

<div class="testimonial-box">
<div class="img-circle"></div>
 <p class="client-name">Neha Aggarwal</p>
</div>
</section>

EDIT

As per the request, the snippet updated in a way that only two letters will be shown in the img-circle. So for "Vipin Kumar Verma", it will be "VV".

about 4 years ago · Juan Pablo Isaza Relatório
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