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

215
Views
¿Cómo puedo obtener la primera letra de cada palabra en Jquery?

Tengo varios nombres y quiero obtener la primera letra o cada palabra. ¿Cómo puedo hacer esto?

Mi código:-

 $('.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>

¡Gracias!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

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 Report

0

Puede usar split para dividir el texto según el espacio. Entonces obtendrá su nombre y segundo nombre.

Entonces puedes tomar las primeras letras de ambas palabras. Fácil.

Si tiene más de 2 palabras en el nombre, puede usar un mapa/forEach para obtener cada letra.

 $('.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>

EDITAR

Según la solicitud, el fragmento se actualizó de manera que solo se mostrarán dos letras en el img-circle . Entonces, para "Vipin Kumar Verma", será "VV".

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