...

View Full Version : adding things to a textarea



theashman
08-11-2008, 07:56 PM
hey im trying to create a simple text editor and im having a bit of trouble inserting things into a textarea. im no JS wiz, infact in quite the opposite.

i did a bit of searching and i cam across this function:

//myField accepts an object reference, myValue accepts the text strint to add
function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();

//in effect we are creating a text range with zero
//length at the cursor location and replacing it
//with myValue
sel = document.selection.createRange();
sel.text = myValue;
}
//Mozilla/Firefox/Netscape 7+ support
else if (myField.selectionStart myField.selectionStart == '0')
{

//Here we get the start and end points of the
//selection. Then we create substrings up to the
//start of the selection and from the end point
//of the selection to the end of the field value.
//Then we concatenate the first substring, myValue,
//and the second substring to get the new value.
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;
}
}

it looks like i would expect the function i need to look and this is my source code:

<form name="addnews" action="/some/processing/script.php" method="post">
Title:<br />
<input type="text" name="title" /><br />
Message:<br />
<button type="button" onclick="insertAtCursor(document.getElementById('textarea'),'')">B</button><br />
<textarea id="textarea" name="news" rows="15" cols="50"></textarea><br />
<input type="submit" name="addnews" value="Add News" /> <a href="index.php">Cancel</a>
</form>

the problem is that the text that i assume will be entered into the textarea is not appearing.
am i doing something wrong?

Thanks

Nile
08-11-2008, 08:02 PM
I tried running this script and using it correctly, it didn't work. So what I suggest is using this(me and a friend made it):


function singleWrap(myField, myValue) {
var obj = document.getElementById(myField);
var start = obj.value.slice(0, obj.selectionStart);
var end = obj.value.slice(obj.selectionEnd, obj.value.length);
var text = start + myValue+' ' + end;
document.getElementById(myField).value = text;
}
function wrapHighlighted(myField, myValue){
var obj = document.getElementById(myField);
var start = obj.value.slice(0, obj.selectionStart);
var end = obj.value.slice(obj.selectionEnd, obj.value.length);
var middle = obj.value.substring(obj.selectionStart, obj.selectionEnd);
var text = start + '['+myValue+']' + middle + '[/'+myValue+'] ' + end;
document.getElementById(myField).value = text;
}

The singleWrap function is used for inserting smileys.
And the wrapHighlightedis used for wrapping 2 things around the highlighted.

Heres the bold tag in your case:


<button type="button" onclick="wrapHighlighted('textarea','b')">B</button>

And to make a smiley:


<button type="button" onclick="singleWrap('textarea',': )')">: )</button>

This code is tested. So it should work.
I hope this helps,
Nile

theashman
08-11-2008, 08:29 PM
thats what i get for trusting google :P

but that works fantastically!

thanks alot!!

Nile
08-11-2008, 09:02 PM
I'm glad to help you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum