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

178
Views
Tomando solo la primera cadena del valor de pegado en el valor de entrada

Quiero obtener solo el primer valor del valor de copiar y pegar.

Por ejemplo: mi texto de copia es: Lorem Lipsum Test

Entonces, cuando copié este texto anterior y lo pegué en la entrada HTML archivada, solo debe aparecer "Lorem" . No necesito otro texto.

¿Hay alguna manera de lograr estas cosas?

Gracias por adelantado.

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

0

Puede agregar un evento de entrada para ver lo que está tratando de agregar a su elemento. Este código dividirá el texto pegado y solo agregará la primera palabra.

 const input = document.querySelector('input'); const log = document.getElementById('values'); input.addEventListener('input', updateValue); var previousValue = ""; function updateValue(e) { log.textContent = e.data; if (e.data && e.inputType == "insertFromPaste") { if (e.data.length > 1) { let val = e.data.split(" ")[0]; let str = previousValue + val; e.target.value = str; } } previousValue = e.target.value; }
 <input placeholder="Enter some text" name="name"/> <p id="values"></p>

about 4 years ago · Juan Pablo Isaza Report

0

solo devuelve siempre la primera palabra de la entrada de cadena y debe borrar para copiar el nuevo valor, luego muestra la primera palabra de la nueva cadena copiada.

 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#getfirstword").on("keyup", function(e) { debugger // do stuff! var str = $(this).val(); //Now i separate them by "|" var str1 = str.split('|'); //Now i want to get the first word of every split-ed sting parts: var firstWord =""; for (var i=0;i<str1.length;i++) { //What to do here to get the first word :) firstWord = str1[i].split(' ')[0]; } $("#input1").val(firstWord); }) }); </script> </head> <body> <input id="getfirstword" /> <input id="input1" disabled/> </body> </html>

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!