Hi everyone ?
I have a problem in the script below
[code=php]<script type=”text/javascript”>
function fBold(){
var obj = document.getElementById(‘txtar’);
var txt = getNewStr(obj, “[B]”, “[/B]”);
obj.value = txt ;
}
function getNewStr(obj, stra, strb){
if(obj.value.length != 0){
if(document.selection) {
//do IE code
return document.selection;
} else if(obj.selectionStart) {
//do FF code
var start = obj.selectionStart;
var end = obj.selectionEnd;
if (start != end){
var newstr = obj.value.substring(0, start) + stra +
obj.value.substring(start, end) + strb +
obj.value.substring(end);
return newstr;
}else{
return obj.value;
}
}
} else{
return ”;
}
}
</script>
The obj is a textarea and the script wraps [B] tags around it. It works fine, as long as I select from the second char or further. When the selection starts at the first char, I get ‘undefined’ in the textarea.
Can anyone solve this problem?
Thanks in advance