Hej Mikkelk
Fik den til at fungerer med den her, som jeg fandt på det link du gav.
Læg et svar, så får du point. Takker for hjælpen :-)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="
http://www.w3.org/1999/xhtml"><head>
<script type="text/javascript">
// globals
var curSelVal = "";
var startPos = 0;
var endPos = 0;
// functions
function storeCurVal(obj, start, end) {
// function not called if IE 4+
startPos = obj.selectionStart;
endPos = obj.selectionEnd;
curSelVal = document.forms[0].copy.value;
}
function getSel(tag)
{
if(document.selection){
oldString = document.selection.createRange().text;
if (oldString != "") {
document.selection.createRange().text = '<' + tag + '>' + oldString + '</' + tag + '>';
}
return false;
} else if(window.getSelection){
oldString = curSelVal;
newString = '';
finalString = '';
len = curSelVal.length; // length of current string
if (len > 0 && document.forms[0].copy.value.length > 0) {
// get from beginning of string to start position
firstPart = oldString.substring(0, startPos);
// get from end of selection to end of total string
lastPart = oldString.substring(endPos, len);
// store the new string
for (i=startPos; i<endPos; i++) {
newString += oldString[i];
}
// surround it with the proper tag
finalString = '<' + tag + '>' + newString + '</' + tag + '>';
// rewrite it back into the textarea
document.forms[0].copy.value = firstPart + finalString + lastPart;
} else {
curSelVal = "";
}
} else if(document.getSelection){ // don't know which browser would actually call this
txt = obj.setSelectionRange(obj.selectionStart, obj.selectionEnd);
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>how to add tags around <textarea> text</title>
</head>
<body>
<form action="" method="post">
<textarea name="copy" id="copy" cols="50" rows="15" onselect="if (!document.selection) storeCurVal(this);"></textarea>
<input type="button" name="selectString" value="add <p>" onclick="getSel('p');" />
</form>
</body>
</html>