G'day guys,
Given that I am (sort of) new to javascript I have the following code;
<html>
<head></head>
<script type="text/javascript">
[INDENT]function insertBold(tArea){
[INDENT]var unselectedText = tArea.value;
var selectedText = tArea.value.substr(tArea.selectionStart, tArea.selectionEnd);
var v = "[B]" + selectedText + "[/B]";
var temp_array = tArea.value.split(selectedText);
tArea.value = v;[/INDENT]
}[/INDENT]
</script>
<body>
[INDENT]<textarea id="mytext" rows="8" cols="70"></textarea>
<button onclick="insertBold(document.getElementById('mytext'))">BOLD</button> [/INDENT]
</body>
</html>
The code above, works as is intended. However, I cannot figure out how to use the split(); function in order to get the text before and after the 'selected' text and recombine it with the addition of the '' tags.
I've tried to use the following but to no success.
var txt_array = unselectedText.split(selectedText);
var part_num = 0;
var t = "";
while (part_num < txt_array.length) {
if( part_num != 1 ) { // if not the selected text (expected length of 3 for array)
t = t + txt_array[part_num];
}else{
t = t + "B" + txt_array[part_num] + "/B";
part_num+=1;
}
}
You help is very much appreciated.
Cheers