Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Vinícius Trentin

Função para cursor

Recommended Posts

O que você está pretendo é adicionar BBCode em textos selecionados de um Textarea.

 

Teste o código a seguir (testado no IE e Firefox):

<html>
<head>
<title>Editor de Texto JavaScript</title>
<script type="text/javascript">
function textarea_selecionado(textarea) {
if(document.selection && document.selection.createRange().text != ""){
document.selection.createRange().text = "[b]" + document.selection.createRange().text + "[/b]";
}
else if(typeof(textarea.selectionStart) != "undefined") {
var antes, selecionado, depois;

antes = textarea.value.substring(0, textarea.selectionStart);
selecionado = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
depois = textarea.value.substring(textarea.selectionEnd);

textarea.value = antes + "[b]" + selecionado + "[/b]" + depois;
}
if(document.selection.createRange().text == "" || (!document.selection && textarea.selectionStart == "undefined")) {
textarea.value = textarea.value + "[b][/b]";
}
}
</script>
 
<body>
<a href="javascript:textarea_selecionado(document.getElementById('texto'));">B</a>
<textarea id="texto" name="texto"></textarea>
</body>
</html>

Referência: http://webcascade.blogspot.com/2008/03/como-pegar-um-texto-selecionado-em-uma.html

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.