Advertisement
MizunoBrasil

Colocar tag de paragrafo no texto usando um botão

Feb 11th, 2023 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <title>Adicionar tags de parágrafo</title>
  6.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  7.   <style>
  8.     textarea {
  9.       width: 100%;
  10.       height: 500px;
  11.     }
  12.   </style>
  13. </head>
  14. <body>
  15.   <div class="container my-5">
  16.     <form>
  17.       <textarea id="textarea"></textarea><br>
  18.      
  19.         <button type="button" class="btn btn-primary mr-2" onclick="addParagraphTags()">Adicionar tags de parágrafo</button>
  20.         <button type="button" class="btn btn-secondary mr-2" onclick="boldSelectedText()">Negrito</button>
  21.         <button type="button" class="btn btn-success mr-2" onclick="copyText()">Copiar para área de transferência</button>
  22.         <button type="button" class="btn btn-info" onclick="addHyperlink()">Hyperlink</button>
  23.      
  24.     </form>
  25.   </div>
  26.   <script>
  27.     function addParagraphTags() {
  28.       var textarea = document.getElementById("textarea");
  29.       var text = textarea.value;
  30.       var paragraphs = text.split("\n");
  31.       for (var i = 0; i < paragraphs.length; i++) {
  32.         paragraphs[i] = "<p>" + paragraphs[i] + "</p>";
  33.       }
  34.       textarea.value = paragraphs.join("\n");
  35.     }
  36.    
  37.     function boldSelectedText() {
  38.       var textarea = document.getElementById("textarea");
  39.       var start = textarea.selectionStart;
  40.       var end = textarea.selectionEnd;
  41.       var selectedText = textarea.value.substring(start, end);
  42.       var newText = textarea.value.substring(0, start) + "<b>" + selectedText + "</b>" + textarea.value.substring(end);
  43.       textarea.value = newText;
  44.     }
  45.    
  46.     function copyText() {
  47.       var textarea = document.getElementById("textarea");
  48.       textarea.select();
  49.       document.execCommand("copy");
  50.       alert("Texto copiado para a área de transferência");
  51.     }
  52.    
  53.     function addHyperlink() {
  54.       var textarea = document.getElementById("textarea");
  55.       var start = textarea.selectionStart;
  56.       var end = textarea.selectionEnd;
  57.       var selectedText = textarea.value.substring(start, end);
  58.       var url = prompt("Informe a URL");
  59.       var textarea = document.getElementById("textarea");
  60. var text = textarea.value;
  61.  
  62. var start = textarea.selectionStart;
  63. var end = textarea.selectionEnd;
  64. var selectedText = text.substring(start, end);
  65.  
  66. var newText = text.substring(0, start) + "<a href='" + url + "' target='_blank'>" + selectedText + "</a>" + text.substring(end);
  67. textarea.value = newText;
  68. }
  69. </script>
  70. </body>
  71. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement