Guest User

Untitled

a guest
Jun 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Find and Replace with String Methods</title>
  6. <meta name="viewport" content="width=device-width">
  7. <link rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <header>
  11. <h1>Find and Replace with String Methods</h1>
  12. </header>
  13. <main>
  14. <form action="#" method="post">
  15. <div>
  16. <label for="find">Find: </label>
  17. <input type="text" id="find" />
  18. </div>
  19. <div>
  20. <label for="replace">Replace: </label>
  21. <input type="text" id="replace" />
  22. </div>
  23. <div>
  24. <button id="btnSearch">Go</button>
  25. </div>
  26. </form>
  27. <p class="target">Lorem apple ipsum dolor Apple sit amet, consectetur apple adipisicing elit. Nihil apple laborum maxime ipsam apple exercitationem apple a blanditiis. Officiis apple necessitatibus esse apple debitis harum apple amet apple molestias sunt adipisci apple iste, voluptates apple vitae, ipsam apple iure, apple odio.</p>
  28. </main>
  29. <script>
  30. document.addEventListener('DOMContentLoaded', function(){
  31. let btn = document.getElementById('btnSearch');
  32. btn.addEventListener('click', doFindAndReplace);
  33. });
  34.  
  35. function doFindAndReplace(ev){
  36. ev.preventDefault();
  37.  
  38. let find = document.getElementById('find').value;
  39. let replace = document.getElementById('replace').value;
  40.  
  41. let p = document.querySelector('.target');
  42.  
  43. while( p.textContent.indexOf(find) != -1 ){
  44. p.textContent = p.textContent.replace(find, replace);
  45. }
  46.  
  47. }
  48. </script>
  49. </body>
  50. </html>
Add Comment
Please, Sign In to add comment