What you're looking for is the contenteditable
attribute that some HTML elements have so that the user can edit their internal content.
Have you seen editors of certain sites that allow you to format the text when you are writing? Some of them use this property. If you can not locate them, the new version of Reddit has one of them when you want to add a new comment, to say an example.
Its use is very simple, and according to the Mozilla Developers Network documentation You must always specify a value for that attribute. I'll explain:
This tag will allow you to insert content within it.
<p contenteditable="true"></p>
This tag will be displayed as a paragraph in your document.
<p contenteditable="false"></p>
But never do this:
<p contenteditable=""></p>
<p contenteditable></p>
For the integration with Javascript and jQuery, I can think of something quick like the following:
let bandera = true; // La bandera cambia su valor entre verdadero y falso cada vez que el usuario hace clic sobre el botón
let funcion = function(evento){
evento.preventDefault(); // Cuando invocamos esta función, le decimos al navegador que no ejecute la acción por defecto de dicho botón y en su lugar proceda con las instrucciones que le damos aquí abajo
jQuery('p').attr('contenteditable', bandera);
bandera = !bandera; // Simplemente intercambiamos su valor anterior
}
jQuery(document).on('click', 'button', funcion);
If you need to do something more complex, consider using some existing libraries like Trumbowyg .