{"id":28811,"date":"2024-04-24T08:02:35","date_gmt":"2024-04-24T07:02:35","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/guia-practica-para-construir-un-editor-de-texto-en-javascript\/"},"modified":"2024-06-03T17:42:40","modified_gmt":"2024-06-03T16:42:40","slug":"guia-practica-para-construir-un-editor-de-texto-en-javascript","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/practical-guide-to-building-a-text-editor-in-javascript\/","title":{"rendered":"Practical Guide to Building a Text Editor in JavaScript"},"content":{"rendered":"<p>Creating a text editor may seem like a complex task, but with JavaScript and a basic understanding of event handling and the Document Object Model (DOM), it can become an exciting and educational project. This article will detail the steps to create a simple text editor from scratch, teaching you essential web programming concepts and giving you a useful tool for your future projects.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/practical-guide-to-building-a-text-editor-in-javascript\/#La_Base_HTML_y_CSS\" >The Base: HTML and CSS<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/practical-guide-to-building-a-text-editor-in-javascript\/#HTML_Estructura_del_Editor\" >HTML: Editor Structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/practical-guide-to-building-a-text-editor-in-javascript\/#CSS_Estilizando_el_Editor\" >CSS: Styling the Editor<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/practical-guide-to-building-a-text-editor-in-javascript\/#JavaScript_El_Corazon_del_Editor\" >JavaScript: The Heart of the Editor<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/practical-guide-to-building-a-text-editor-in-javascript\/#Accediendo_a_Elementos_del_DOM\" >Accessing DOM Elements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/practical-guide-to-building-a-text-editor-in-javascript\/#Manejando_Eventos\" >Managing Events<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/practical-guide-to-building-a-text-editor-in-javascript\/#Guardando_Texto_Localmente\" >Saving Text Locally<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/practical-guide-to-building-a-text-editor-in-javascript\/#Refinando_y_Expandiendo\" >Refining and Expanding<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"La_Base_HTML_y_CSS\"><\/span>The Base: HTML and CSS<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before we dive into JavaScript, we need to prepare the groundwork with some HTML and CSS. We will start by creating a file <code>index.html<\/code> and a <code>styles.css<\/code>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"HTML_Estructura_del_Editor\"><\/span>HTML: Editor Structure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"&quot;language-html&quot;\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;es&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;estilos.css&quot;&gt;\n    &lt;title&gt;Simple Text Editor&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div id=&quot;editor&quot;&gt;\n        &lt;textarea id=&quot;areaTexto&quot;&gt;&lt;\/textarea&gt;\n        &lt;button id=&quot;botonGuardar&quot;&gt;Keep&lt;\/button&gt;\n    &lt;\/div&gt;\n    &lt;script src=&quot;script.js&quot;&gt;&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"CSS_Estilizando_el_Editor\"><\/span>CSS: Styling the Editor<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In the File <code>styles.css<\/code>, let&#039;s add some styling to make our editor more attractive.<\/p>\n<pre><code class=\"&quot;language-css&quot;\">#editor { width: 80%; margin: self; padding: 20px; } #askText { width: 100%; height: 200px; } #SaveButton { display: block; width: 100px; margin: 20px auto; padding: 10px; background-color: #4CAF50; colour: white; font-size: 16px; border:none; cursor: pointer; }<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"JavaScript_El_Corazon_del_Editor\"><\/span>JavaScript: The Heart of the Editor<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Now that our interface is ready, it&#039;s time to bring it to life with JavaScript. Let&#039;s create a file <code>script.js<\/code> to handle DOM events, such as capturing the entered text and saving the content when the button is pressed.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Accediendo_a_Elementos_del_DOM\"><\/span>Accessing DOM Elements<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>First, we need to get a reference to the HTML elements we will be interacting with.<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">const textarea = document.getElementById(&#039;textarea&#039;); const savebutton = document.getElementById(&#039;savebutton&#039;);<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Manejando_Eventos\"><\/span>Managing Events<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To make our editor functional, we will add an event listener to the button to save the text when clicked.<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">savebutton.addEventListener(&#039;click&#039;, function() { const text = textarea.value; localStorage.setItem(&#039;savedtext&#039;, text); alert(&#039;SavedText!&#039;); });<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Guardando_Texto_Localmente\"><\/span>Saving Text Locally<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In this example, we are using <code>localStorage<\/code> to save the text. It is a simple and fast way to store data without the need for a database.<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">const savedText = localStorage.getItem(&#039;savedText&#039;); if (savedText !== null) { textarea.value = savedText; }<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Refinando_y_Expandiendo\"><\/span>Refining and Expanding<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>With the fundamentals in place, we can improve and expand our editor. Some ideas include:<\/p>\n<ul>\n<li><strong>Editing Features<\/strong>: Implements options to make bold, italic or underline.<\/li>\n<li><strong>Storage best practices<\/strong>: Use databases or backends like Firebase to store the data.<\/li>\n<li><strong>Multiple file support<\/strong>: Allows users to create and save multiple text files.<\/li>\n<\/ul>\n<p>This basic guide provides an excellent launching pad for any web programming enthusiast. From here, the possibilities are vast, and you now have the tools to start exploring them.<\/p>\n<p>For more tutorials and tips, visit my blog at <a href=\"https:\/\/nelkodev.com\/en\/\">NelkoDev<\/a> and if you have any questions or need additional help, feel free to contact me via <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">contact<\/a>. Happy coding!<\/p>","protected":false},"excerpt":{"rendered":"<p>Creating a text editor may seem like a complex task, but with JavaScript and a basic understanding of event handling and the Document Object Model (DOM), it can become an exciting and educational project. This article will detail the steps to create a simple text editor from scratch, teaching you essential concepts of how to use it.<\/p>","protected":false},"author":1,"featured_media":28812,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,1901],"tags":[205,134,1951,358,185,60,1214,627,1008,736],"class_list":["post-28811","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-pruebas-de-proyecto","tag-blog","tag-construir","tag-editor","tag-guia","tag-javascript","tag-para","tag-practica","tag-proyecto","tag-pruebas","tag-texto"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=28811"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28811\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/28812"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=28811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=28811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=28811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}