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

172
Visualizações
Taking only first string from paste value in input value

I want to get only the first value from copy-paste value.

For example: my copy text is: Lorem Lipsum Test

So when I copied this above text and paste it into the HTML input filed there need to come only "Lorem" I don't need other text

Is there any way to achieve these things?

Thanks in advance.

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

0

You can add an input event to see what you are trying to add to your element. This code will split your pasted text and only add the first word.

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 Relatório

0

it only returns always the first word from string input and you have to clear for copy new value then it's show first word of the new copied string.

<!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 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