Revos Posted January 26, 2007 Share Posted January 26, 2007 I got this code from this site and here it works GREAT in both FF and IE.[code]function surroundText(text1, text2, textarea){ // Can a text range be created? if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) { var caretPos = textarea.caretPos; var temp_length = caretPos.text.length; caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2; if (temp_length == 0) { caretPos.moveStart("character", -text2.length); caretPos.moveEnd("character", -text2.length); caretPos.select(); } else textarea.focus(caretPos); } // Mozilla text range wrap. else if (typeof(textarea.selectionStart) != "undefined") { var begin = textarea.value.substr(0, textarea.selectionStart); var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart); var end = textarea.value.substr(textarea.selectionEnd); var newCursorPos = textarea.selectionStart; var scrollPos = textarea.scrollTop; textarea.value = begin + text1 + selection + text2 + end; if (textarea.setSelectionRange) { if (selection.length == 0) textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length); else textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length); textarea.focus(); } textarea.scrollTop = scrollPos; } // Just put them on the end, then. else { textarea.value += text1 + text2; textarea.focus(textarea.value.length - 1); }}[/code]I use it so:[code]<input type="button"onClick="window.location.href='javascript:void(0)'; surroundText('[I]', '[/I]', document.FormPost.Post); return false;"value=" I" style="border-right-width:1px;border-bottom-width:1px; border-top-width:1px;border-left-width:1px;font-family:Arial; font-style:italic; font-size:12px" /> [/code]Why it is not working it Internet Explorer?In FireFox it is working GREAT! Quote Link to comment Share on other sites More sharing options...
fenway Posted January 26, 2007 Share Posted January 26, 2007 Which part? Quote Link to comment Share on other sites More sharing options...
Revos Posted January 26, 2007 Author Share Posted January 26, 2007 it is not going to this part:[code]if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) { var caretPos = textarea.caretPos; var temp_length = caretPos.text.length; caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2; if (temp_length == 0) { caretPos.moveStart("character", -text2.length); caretPos.moveEnd("character", -text2.length); caretPos.select(); } else textarea.focus(caretPos); }[/code]it is skiping this and going to the last ELSE part! Quote Link to comment Share on other sites More sharing options...
fenway Posted January 26, 2007 Share Posted January 26, 2007 Test each piece of that if statement separately. Quote Link to comment Share on other sites More sharing options...
Revos Posted January 27, 2007 Author Share Posted January 27, 2007 let me tell you what happens:[code]if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) { var caretPos = textarea.caretPos; var temp_length = caretPos.text.length; caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2; if (temp_length == 0) { caretPos.moveStart("character", -text2.length); caretPos.moveEnd("character", -text2.length); caretPos.select(); } else textarea.focus(caretPos); }[/code]the IF part suppost to check if it's IE but when I use IE it doesn't do the IF part and goes to the last ELSE part, why?! Quote Link to comment Share on other sites More sharing options...
Revos Posted January 27, 2007 Author Share Posted January 27, 2007 ok let me change my question, what it wrong in this code in IE?[code]if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) { var caretPos = textarea.caretPos; var temp_length = caretPos.text.length; caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2; if (temp_length == 0) { caretPos.moveStart("character", -text2.length); caretPos.moveEnd("character", -text2.length); caretPos.select(); } else textarea.focus(caretPos); }[/code] Quote Link to comment Share on other sites More sharing options...
fenway Posted January 30, 2007 Share Posted January 30, 2007 So it does get into the this if block, or it doesn't? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.