Jump to content
rtfmplay

javascript

Recommended Posts

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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