par
Genova » 08 févr. 2007, 12:50
Bonjour,
essaie avec ce code :
Code : Tout sélectionner
/*
** Insertion de texte dans un textarea
** -----
** id :: ID du textarea
** open :: Ouverture
** defaultText :: Texte central
** close :: Fermeture
*/
function launch_insert_text(id, open, defaultText, close)
{
var txtarea = document.getElementById(id);
txtarea.focus();
// IE support
if (document.selection)
{
insert_ie(open, defaultText, close, txtarea);
}
// MOZILLA support
else if (txtarea.selectionStart || txtarea.selectionStart == '0')
{
insert_mozilla(open, defaultText, close, txtarea);
}
else
{
txtarea.value += open + defaultText + close;
}
}
function insert_ie(open, defaultText, close, txtarea)
{
if (txtarea.createTextRange)
{
txtarea.focus(txtarea.caretPos);
txtarea.caretPos = document.selection.createRange().duplicate();
if (txtarea.caretPos.text.length > 0 && open != '')
{
defaultText = txtarea.caretPos.text;
}
txtarea.caretPos.text = open + defaultText + close;
}
}
function insert_mozilla(open, defaultText, close, txtarea)
{
var x = txtarea.scrollTop;
var selStart = txtarea.selectionStart;
var selEnd = txtarea.selectionEnd;
var selLength = selEnd - selStart;
var textStart = txtarea.value.substring(0,selStart);
var textEnd = txtarea.value.substring(selEnd, txtarea.textLength);
if (selLength != 0 && open != '')
{
defaultText = (txtarea.value).substring(selStart, selEnd)
}
if (x == 0 && (txtarea.textLength == selStart))
{
x = txtarea.textLength + 200;
}
txtarea.value = textStart + open + defaultText + close + textEnd;
var txt = open + defaultText + close;
var cur_pos = selStart + txt.length;
txtarea.scrollTop = x;
if (!(selLength != 0 && open != ''))
{
txtarea.selectionStart = selStart + open.length;
txtarea.selectionEnd = txtarea.selectionStart + defaultText.length;
}
else
{
txtarea.selectionStart = selStart + open.length;
txtarea.selectionEnd = selEnd + open.length;
}
}
pour l'utilisation :
Code : Tout sélectionner
<textarea id="id_du_textarea"></textarea><br />
<input type="button" value="Gras" onclick="launch_insert_text('id_du_textarea', '[b]', 'Texte par defaut', '[/b]')" />
Bonjour,
essaie avec ce code :
[code]/*
** Insertion de texte dans un textarea
** -----
** id :: ID du textarea
** open :: Ouverture
** defaultText :: Texte central
** close :: Fermeture
*/
function launch_insert_text(id, open, defaultText, close)
{
var txtarea = document.getElementById(id);
txtarea.focus();
// IE support
if (document.selection)
{
insert_ie(open, defaultText, close, txtarea);
}
// MOZILLA support
else if (txtarea.selectionStart || txtarea.selectionStart == '0')
{
insert_mozilla(open, defaultText, close, txtarea);
}
else
{
txtarea.value += open + defaultText + close;
}
}
function insert_ie(open, defaultText, close, txtarea)
{
if (txtarea.createTextRange)
{
txtarea.focus(txtarea.caretPos);
txtarea.caretPos = document.selection.createRange().duplicate();
if (txtarea.caretPos.text.length > 0 && open != '')
{
defaultText = txtarea.caretPos.text;
}
txtarea.caretPos.text = open + defaultText + close;
}
}
function insert_mozilla(open, defaultText, close, txtarea)
{
var x = txtarea.scrollTop;
var selStart = txtarea.selectionStart;
var selEnd = txtarea.selectionEnd;
var selLength = selEnd - selStart;
var textStart = txtarea.value.substring(0,selStart);
var textEnd = txtarea.value.substring(selEnd, txtarea.textLength);
if (selLength != 0 && open != '')
{
defaultText = (txtarea.value).substring(selStart, selEnd)
}
if (x == 0 && (txtarea.textLength == selStart))
{
x = txtarea.textLength + 200;
}
txtarea.value = textStart + open + defaultText + close + textEnd;
var txt = open + defaultText + close;
var cur_pos = selStart + txt.length;
txtarea.scrollTop = x;
if (!(selLength != 0 && open != ''))
{
txtarea.selectionStart = selStart + open.length;
txtarea.selectionEnd = txtarea.selectionStart + defaultText.length;
}
else
{
txtarea.selectionStart = selStart + open.length;
txtarea.selectionEnd = selEnd + open.length;
}
}[/code]
pour l'utilisation :
[code]<textarea id="id_du_textarea"></textarea><br />
<input type="button" value="Gras" onclick="launch_insert_text('id_du_textarea', '[b]', 'Texte par defaut', '[/b]')" />[/code]