Forum Moderators: open
I was wondering if there was a way of being able to highlight text that is inside a text box/field and then affect this text then insert it back in the same position?
currently i am only able to add text at the end of my text box/field using javascript.
Here is what i want to do.. (this is inside a textbox).
some text that i have written
I want to highlight the word "that" and then insert <strong>that</strong> in its place instead..
Any ideas?
Cheers
Ally
I will place the code below if anyone wants to use it.
This assumes that your form is called TheForm and your text field is called TheText..
function insertTag()
{
var totalString; //store original string so that we can manipulated
var selectedString; //the selected string
var totalLength; //total length of the string
var selectedLength; //length of the selected area
var indexStart; //index of the string selected
var indexFinished; //last index of the string selected
var firstTextValue; //the first part of the string before index
var lastTextValue; //the last part of the string after index
totalString = document.TheForm.TheText.value;
selectedString = document.selection.createRange().text;
totalLength = document.TheForm.TheText.value.length;
selectedLength = document.selection.createRange().text.length;
indexStart = totalString.indexOf(selectedString);
indexFinished = indexStart + selectedLength;
firstTextValue = totalString.substring(0, indexStart);
lastTextValue = totalString.substring(indexFinished, totalLength);
selectedString = "<b>"+selectedString+"</b>";
document.TheForm.TheText.value = firstTextValue + selectedString + lastTextValue;
}