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

130
Views
Cómo agregar más una posición a setSelectionRange

Estoy tratando de mover el cursor una posición más a la derecha en la entrada.

 var start = this.selectionStart, end = this.selectionEnd; this.setSelectionRange(start, end);

Esto funciona para mantener la posición en el mismo lugar después de cambiar el valor de entrada, pero estoy tratando de agregarle una posición más, porque mi código de formato agrega el carácter "-" después de 4 caracteres.

 $("input[name=login-access-token]").keypress(function(event){ var start = this.selectionStart, end = this.selectionEnd; console.log(start); var input = $(this).val(); input = input.replace(/[\W\s\._\-]+/g, ''); var split = 4; var chunk = []; for (var i = 0, len = input.length; i < len; i += split) { split = 4; chunk.push( input.substr( i, split ) ); } var test = []; chunk.forEach(element => { test.push(element.match(/.{1,4}/g)); }); test.forEach(element => { if(element.length!=4){ $(this).val(function() { var out = chunk.join("-").toUpperCase(); if(out.length == 4 && event.keyCode != 8){ out += "-"; } if(out.length == 9 && event.keyCode != 8 ){ out += "-"; } return out; }); } }); this.setSelectionRange(start, end); var selection = window.getSelection(); selection.addRange(1); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <input type="text" name="login-access-token" placeholder="Code" />

 var selection = window.getSelection(); selection.addRange(1); //not working :/
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

¡Lo resolví! Simplemente agregue +1 al inicio y al final, que son los valores que obtenemos de selectionStart.

 if(out.length == 4 && event.keyCode != 8){ out += "-"; start = start+1; end = end +1; } if(out.length == 9 && event.keyCode != 8 ){ out += "-"; start = start+1; end = end +1; }
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!