Jump to content
rtfmplay

javascript

Recommended Posts

Posted

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

Posted

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>

Posted

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.

Posted
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?

 

Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...