calabiyau Posted February 4, 2007 Share Posted February 4, 2007 Okay I got the javascript code from someone else and all the comments said it worked. I am trying to do this for content management site, use the javascript to insert tags into the text area, but all I get is "error on page" warning in bottom left of internet explorer 6.0 and nothing happens to the text area. Anyone have any ideas? <script type="text/javascript"> function insertAtCursor(myField, myValue) { //IE support if (document.selection) { myField.focus(); sel = document.selection.createRange(); sel.text = myValue; } //MOZILLA/NETSCAPE support else if (myField.selectionStart || myField.selectionStart == ‘0') { var startPos = myField.selectionStart; var endPos = myField.selectionEnd; myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length); } else { myField.value += myValue; } } </script> and here is the call within the php page [code] <?php if ($_GET['flag']==1) { echo '<form name="softarea" action="'.$current_url.'" method="post">'; echo '<textarea name="softstring" >This is just some sample text</textarea>'; echo '<input type="hidden" name="flag" value="1a"/>'; echo '</form>'; echo '<a href="#" onclick="insertAtCursor(document.softarea.softstring,'."'<h3></h3>'".');">insert h3</a>'; } [/code] Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 4, 2007 Share Posted February 4, 2007 Try this line: echo '<a href="#" onclick="insertAtCursor(\'document.softarea.softstring\','."'<h3></h3>'".');">insert h3</a>'; IE's handling of JS is buggy and offers no debugging support. Sucks. Quote Link to comment Share on other sites More sharing options...
calabiyau Posted February 4, 2007 Author Share Posted February 4, 2007 Thanks for your help. That did stop the error message, unfortunately it still doesn't do anything about inserting the br tags. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 4, 2007 Share Posted February 4, 2007 Is it on a public site where I could possibly see it live? Quote Link to comment Share on other sites More sharing options...
calabiyau Posted February 5, 2007 Author Share Posted February 5, 2007 Nah, it's just on my own development server right now. Thanks for offering to dig deeper though. I will keep searching around and plugging away I guess. I'm pretty good at php but don't know much javascript unfortunately. Quote Link to comment Share on other sites More sharing options...
calabiyau Posted February 5, 2007 Author Share Posted February 5, 2007 Okay if you are reading still, maybe this will help to narrow the problem down. I have put an alert box at the very top of the function in the javascript but when clicking on the link that calls the function, no alert comes up. So there is something wrong with my function call? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 5, 2007 Share Posted February 5, 2007 The only problem that I saw in your original code was an incorrect quote in this line: else if (myField.selectionStart || myField.selectionStart == ‘0') { Change the backtick to a single quote: else if (myField.selectionStart || myField.selectionStart == '0') { Also, although it's not an error, change: <?php echo '<form name="softarea" action="'.$current_url.'" method="post">'; ?> to <?php echo '<form name="softarea" action="'.$_SERVER['PHP_SELF'].'" method="post">'; ?> I made the change of the quote and the code worked fine. Ken Quote Link to comment Share on other sites More sharing options...
calabiyau Posted February 5, 2007 Author Share Posted February 5, 2007 Did it actually update the text area the way it was supposed to? I made the change that previous poster mentioned by enclosing the first function parameter in escaped quotes and made the change with the single quote error within the script. Now the alert boxes are coming up to say I have gotten inside the functions but it is still not making any change to the text area. Here is the code now: echo '<form id="softarea" action="'.$current_url.'" method="post">'; echo '<textarea id="softstring" >This is just some sample text</textarea>'; echo '<input type="hidden" name="flag" value="1a"/>'; echo '</form>'; echo '<a href="#" onclick="insertAtCursor(\'document.softarea.softstring\','."'<h3></h3>'".');">insert h3</a>'; echo '<a href="#" onClick="testing( );">testing</a>'; Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 5, 2007 Share Posted February 5, 2007 Here's my code. Yes it puts up the text box and clicking the like inserts the text. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <script type="text/javascript"> function insertAtCursor(myField, myValue) { //IE support if (document.selection) { myField.focus(); sel = document.selection.createRange(); sel.text = myValue; } //MOZILLA/NETSCAPE support else if (myField.selectionStart || myField.selectionStart == '0') { var startPos = myField.selectionStart; var endPos = myField.selectionEnd; myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length); } else { myField.value += myValue; } } </script> </head> <body> <?php if ($_GET['flag']==1) { echo '<form name="softarea" action="'.$_SERVER['PHP_SELF'].'" method="post">'; echo '<textarea name="softstring" >This is just some sample text</textarea>'; echo '<input type="hidden" name="flag" value="1a"/>'; echo '</form>'; echo '<a href="#" onclick="insertAtCursor(document.softarea.softstring,'."'<h3></h3>'".');">insert h3</a>'; } ?> </body> </html> Ken Quote Link to comment Share on other sites More sharing options...
calabiyau Posted February 5, 2007 Author Share Posted February 5, 2007 Yay! It works beautifully now! Thank you so much for taking the time to walk me throught his 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.