mike12255 Posted September 4, 2009 Share Posted September 4, 2009 so im trying to get the text the user selects and add bold tags around them problem is my button dosnt seem to work, can anyone figure out why? <html> <head> <script language="javascript" type="text/javascript"> function ShowSelection() { var textComponent = document.getElementById('outputtext'); var selectedText; // IE version if (document.selection != undefined) { textComponent.focus(); var sel = document.selection.createRange(); selectedText = sel.text; document.myform.outputtext.value += "[b]" + selectedText + "[/b]"; } // Mozilla version else if (textComponent.selectionStart != undefined) { var startPos = textComponent.selectionStart; var endPos = textComponent.selectionEnd; selectedText = textComponent.value.substring(startPos, endPos) document.myform.outputtext.value += "[b]" + selectedText + "[/b]"; } // alert("You selected: " + selectedText); } </script> <form name="myform"> <input type="button" value="Option One" onClick="addtext1();"> <input type="button" value="Bold" onClick="ShowSelection();"> <textarea name="outputtext" cols="30" rows="25">Dear </textarea> </form> <!--next--> </body> </html> Link to comment https://forums.phpfreaks.com/topic/173047-solved-getting-selected-text/ Share on other sites More sharing options...
cbolson Posted September 5, 2009 Share Posted September 5, 2009 Hi, not sure that it is the only problem with the script, but you are defining the textComponent by the id, however this is not defined. var textComponent = document.getElementById('outputtext'); For your textarea you have nly goiven it a name, if you want to use getElementById() you need to define the id attribute as well. <textarea id="outputtext" name="outputtext" cols="30" rows="25">Dear </textarea> Hope this helps you on your way Chris Link to comment https://forums.phpfreaks.com/topic/173047-solved-getting-selected-text/#findComment-913068 Share on other sites More sharing options...
mike12255 Posted September 6, 2009 Author Share Posted September 6, 2009 sadly i always make small little mistakes like that but cant catch them other then that my script does as i wish, thanks for catching the error mate Link to comment https://forums.phpfreaks.com/topic/173047-solved-getting-selected-text/#findComment-913421 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.