rtfmplay Posted March 30, 2017 Report Posted March 30, 2017 Salut, nu prea m-am intersectat cu javascript si vreau sa intreb ceva legat de el. Trebuie sa fac replace la niste caractere introduse de la tastatura asupra unui input de tip text. Sa inlocuiasca in timp de userul introduce "-" cu "/" dar neaprat in timp de introduce textul; 23gg3j3b-3dd9ds-ossd33yj-oopw3 23gg3j3b/3dd9ds/ossd33yj/oopw3 Quote
costi Posted March 31, 2017 Report Posted March 31, 2017 Prima data, adaugi un event listener pe input (keyup, keydown, ..), apoi aplici un callback care iti formateaza output ul. Pentru functia care iti formateaza output ul poti folosi si expresii regulate (nivel avansat ), apoi faci append in DOM. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Find and Replace Dashes</title> </head> <body> <form action="" class=""> <input type="text" class="target"> </form> <script type="text/javascript"> let input = document.getElementsByClassName('target')[0]; let newNode = document.createElement('p'); String.prototype.replaceAllDashes = function(search, replacement) { let target = this; return target.split(search).join(replacement); }; input.addEventListener('keyup', function() { newNode.innerHTML = this.value.replaceAllDashes('-', '/'); document.body.appendChild(newNode); }); </script> </body> </html> Quote
pr00f Posted March 31, 2017 Report Posted March 31, 2017 Alternativa cu jQuery (function() { $('.dash-to-slash').on('keyup', function (e) { $(this).val($(this).val().replace(/-/, '/')); }); })() @costi naming-ul tau spune 'replaceAllDashes', dar ii dai dash-ul ca parametru. Plus ca nu rescrii input-ul, ci-l adaugi in DOM. Quote
costi Posted March 31, 2017 Report Posted March 31, 2017 @proof Buna observatie. Exprimarea mea lasa de dorit. Rescris in jQuery, arata mai curat codul. Quote
DoubleG Posted April 1, 2017 Report Posted April 1, 2017 18 hours ago, costi said: @proof Buna observatie. Exprimarea mea lasa de dorit. Rescris in jQuery, arata mai curat codul. dar daca nu foloseste jQuery? incarci o librarie imensa care-ti consuma timp la incarcarcarea paginii si te claseaza mai prost in SEO? Quote
costi Posted April 1, 2017 Report Posted April 1, 2017 Desi inca mai folosesc jQuery din cand in cand, prefer sa folosesc Javascript pur. La capitolul SEO sunt praf.. Quote
DoubleG Posted April 1, 2017 Report Posted April 1, 2017 8 minutes ago, costi said: Desi inca mai folosesc jQuery din cand in cand, prefer sa folosesc Javascript pur. La capitolul SEO sunt praf.. ei bine, cu cat se incarca pagina mai repede, cu atat e mai bine pt SEO. Quote