Good morning,
I still have a theme that is making my head break, but I do not hit the key. In a text editor like the one I am writing now, I have some icons, which when touched, are added some labels in the textarea. But I have a series of problems:
-
The labels are put in order as I press, this a priori well, but it is not practical or functional, since the normal thing is often to write a paragraph and then select the text you want to put in bold.
-
Even if you select a piece of text, the labels appear at the ends of ALL the text.
I have searched for information and in principle you can work with window.getSelection()
, I have tried some script, but it has not just given me any results.
Just as I have the code now, when you click on a button, the text is bold, for example, a script is executed, which receives 3 parameters, one of them is the text, but not the selected text , but the text of all the textarea, I think here is the problem, that I have to send only the selected text.
HTML Code:
<form class="hilo_wrapper" action="crear_hilo.php?foro=<?php echo str_replace(" ", "%",$foro) ?>&subforo=<?php echo $subforo ?>" method="post">
<div class="data_hilo">
<input type="text" name="asunto" placeholder="Asunto:">
</div>
<div class="botones_crear_hilo">
<button type="button" accesskey="b" data-code="[b]" data-close="[/b]" onclick="insertText('txt1', '[b]','[/b]');" title="Negrita: [b]texto[/b] (Alt+B)">
<i class="fa fa-bold fa-fw"></i>
</button>
<button type="button" accesskey="i" data-code="[i]" data-close="[/i]" onclick="insertText('txt1', '[i][/i]');" title="Cursiva: [i]texto[/i] (Alt+I)">
<i class="fa fa-italic fa-fw"></i>
</button>
<button type="button" accesskey="u" data-code="[u]" data-close="[/u]" onclick="insertText('txt1', '[u][/u]');" title="Subrayado: [u]texto[/u] (Alt+U)">
<i class="fa fa-underline fa-fw"></i>
</button>
<button type="button" accesskey="q" data-code="[quote]" data-close="[/quote]" onclick="insertText('txt1', '[quote][/quote]');" title="Cita: [quote]texto[/quote] (Alt+Q)">
<i class="fa fa-quote-right fa-fw"></i>
</button>
<button type="button" accesskey="c" data-code="[code]" data-close="[/code]" onclick="insertText('txt1', '[code][/code]');" title="Código: [code]texto[/code] (Alt+C)">
<i class="fa fa-code fa-fw"></i>
</button>
<button type="button" name="img" accesskey="p" onclick="insertText('txt1', '[img][/img]');" title="Imagen: [img]http://www.ejemplo.com/imagen.jpg[/img] (Alt+P)">
<i class="fa fa-picture-o fa-fw"></i>
</button>
<button type="button" name="url" accesskey="w" onclick="insertText('txt1', '[url][/url]');" title="URL: [url]http://www.ejemplo.com[/url] o [url=http://www.ejemplo.com]texto[/url] (Alt+W)">
<i class="fa fa-link fa-fw"></i>
</button>
<button type="button" accesskey="s" data-code="[spoiler]" data-close="[/spoiler]" onclick="insertText('txt1', '[spoiler][/spoiler]');" title="Spoiler: [spoiler]texto[/spoiler]">
<i class="fa fa-list-alt fa-fw"></i>
</button>
<button type="button" name="mention" title="Mención">
<span class="fa-fw" onclick="insertText('txt1', '@');" style="display: inline-block">@</span>
</button>
</div>
<div class="col-100">
<textarea id="txt1" name="texto_hilo" placeholder="Texto:"><?php if (isset($_POST['vista_previa'])) { echo $vista_previa;} ?></textarea>
<div class="col-emoticonos-enviar">
<input type="submit" name="vista_previa" value="Vista previa">
<input type="submit" name="guardar_borrador" value="Guardar borrador">
<input type="submit" name="crear_hilo" value="Crear hilo">
</div>
</div>
</form>
Javascript Code:
<script type="text/javascript">
function insertText(elemID, first, second) {
var elem = document.getElementById(elemID);
elem.value = second ? first + elem.value + second : first + elem.value;
}
</script>
I would appreciate any help.