PDA

View Full Version : Add text to the current cursor location in textbox



BarrMan
Mar 4th, 2007, 04:45 PM
Hey,
How can I add text to a textbox like the editor in here?
Like the bold, Colors, Underline, Quote, etc.
When you can simply quick an option in the editor and it will automatically be added to the place where the cursor was last.

Thanks.

rwedge
Mar 4th, 2007, 08:44 PM
The phpMyAdmin basic script :
<script type="text/javascript">
<!-- /*<![CDATA[*/
function insertAtCursor(myField, myValue) {
// alert(myField+myValue);
// IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
// MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == 0) {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos,myField.value.length);

}
else {
myField.value += myValue;
}
}
/*]]>*/
// -->
</script>


<form name="f" id="f" method="post" action="" enctype="application/x-www-form-urlencoded">
<textarea name="txt" id="txt" cols="50" rows="5"> this is the text to insert into </textarea><br />
<input type="text" name="test" id="test" value="YaBa YaBa Doo" size="50" maxlength="300" /><br />
<input type="button" name=".b" id=".b" value=" Insert Text " onClick="insertAtCursor(document.f.txt,document.f.test.value)" />
</form> was modified at alexking.org and is available here (http://alexking.org/blog/2004/06/03/js-quicktags-under-lgpl/)

BarrMan
Mar 5th, 2007, 03:26 PM
Hey, Thanks! That works great!
One more thing:
If I want to insert a tag like and I want the cursor to land inbetween the tags. How can I do that?
I've tried making the code myself but failed:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
function populate()
{
var txt = document.getElementById("txt");
txt.focus();
if(document.selection)
{ var sel = document.selection.createRange();
if(!sel.text)
sel.text = "";
else
{
sel.text = "" + sel.text + "";
sel = sel.createTextRange();
sel.moveStart("character",0);
sel.moveEnd("character",len(sel.text));

}
}
}
</script>

</head>
<body>
<input id="txt" value="Test, Test! test?" /><br />
<input type="button" name="" value="Add P" onclick="populate();">
</body>
</html>


Thanks again rwedge, that helped alot!

BarrMan
Mar 6th, 2007, 01:07 PM
Bump.

BarrMan
Mar 7th, 2007, 11:15 AM
Any clue guys?

BarrMan
Mar 8th, 2007, 03:15 PM
I'll bump once again.
I really have no clue. Maybe anyone could tell me what function, word to look for in google?