php_begins Posted August 23, 2011 Share Posted August 23, 2011 Does anyone know how to set focus to the last character of a textarea in javascript? Quote Link to comment https://forums.phpfreaks.com/topic/245522-ast-character-of-a-textarea-in-javascript/ Share on other sites More sharing options...
Adam Posted August 24, 2011 Share Posted August 24, 2011 Normally I would try and give you clues first, but selection ranges are a bugger. I chucked this together instead... function focusEnd(element) { var length = element.value.length; if (!element.createTextRange) { element.focus(); element.selectionStart = length; element.selectionEnd = length; } else { var range = element.createTextRange(); range.moveStart('character', length); range.moveEnd('character', length); range.select(); } } Verified in IE6 / FF / Chrome.. Should do the trick. Just pass it the textarea's element object: focusEnd(document.getElementById('myTextarea')); focusEnd(document.myForm.myTextarea); Quote Link to comment https://forums.phpfreaks.com/topic/245522-ast-character-of-a-textarea-in-javascript/#findComment-1261353 Share on other sites More sharing options...
php_begins Posted August 24, 2011 Author Share Posted August 24, 2011 Thanks, i am quite new to javascript. Can I pass that function with another function that I have used? or is there a different way of calling that? the focus part does not work on IE 8 though.. here is my code.. <script type="text/javascript"> function setComments(userID) { document.getElementById("comments_content").value = document.getElementById("comments_content").value + userID + "\n\n"; } </script> <a onclick="setComments('<?php print $comment_username; ?>') + focusEnd('comments_content')" href="#addcomment"> <img src="images/reply-button.png" align="top" border="0"> </a> <textarea class='comment-input-textarea-loggedin' id='comments_content' name='comments_content'></textarea> Quote Link to comment https://forums.phpfreaks.com/topic/245522-ast-character-of-a-textarea-in-javascript/#findComment-1261381 Share on other sites More sharing options...
php_begins Posted August 24, 2011 Author Share Posted August 24, 2011 ok it kind of works in other browsers except IE and firefox. Is it possible for the function to ignore line breaks? just focus on the last character it sees. if a word ends with a line break the focus would still be in the last character. Quote Link to comment https://forums.phpfreaks.com/topic/245522-ast-character-of-a-textarea-in-javascript/#findComment-1261406 Share on other sites More sharing options...
Adam Posted August 24, 2011 Share Posted August 24, 2011 You need to pass it the element's object, like in the examples I showed you. You can't pass just the ID unless you add the code to get the object from the ID, but then it kind of reduces the reusability of the function. As for it not working, can you provide the version of IE/FF and what text you're trying it with? I can't get it to trip up. Lastly, ignoring line-breaks is more about string manipulation really, you just need to test what's at the end of the value and reduce it accordingly. Have a bash and see what you can do. Quote Link to comment https://forums.phpfreaks.com/topic/245522-ast-character-of-a-textarea-in-javascript/#findComment-1261421 Share on other sites More sharing options...
php_begins Posted August 24, 2011 Author Share Posted August 24, 2011 Ok its working as I wanted . its not working in IE 8 and firefox though...I tried using a setTimeout function to see if it makes any difference in IE and firefox. It does not even focus on the text area,let alone focus where I want it to.. <a onclick="lineBreak() + setComments('<?php print "@".$comment_username; ?>') + setTimeout(focusEnd(document.getElementById('comments_content')),200) Quote Link to comment https://forums.phpfreaks.com/topic/245522-ast-character-of-a-textarea-in-javascript/#findComment-1261425 Share on other sites More sharing options...
Adam Posted August 24, 2011 Share Posted August 24, 2011 Ah, it's because you're using an anchor. Someone had this same problem the other day.. You need to use an image, styled text, etc. Something that doesn't take focus away from the textarea unlike an anchor. Quote Link to comment https://forums.phpfreaks.com/topic/245522-ast-character-of-a-textarea-in-javascript/#findComment-1261428 Share on other sites More sharing options...
php_begins Posted August 24, 2011 Author Share Posted August 24, 2011 But i need the anchor to take me to an # link where the focus would be.. Like <a href=#link>. How would I set the focus to the text area if I do not put my function inside the <a onclick=""> tag ? Quote Link to comment https://forums.phpfreaks.com/topic/245522-ast-character-of-a-textarea-in-javascript/#findComment-1261431 Share on other sites More sharing options...
Adam Posted August 24, 2011 Share Posted August 24, 2011 Actually, I just did a test and that's not the case. The problem the other day was with retaining the document's selection after a link is clicked - this is different. I've tested with IE6 (don't have any later versions available to test with right now), Chrome13 and FF6. All work fine for me regardless. As the selectionStart and selectionEnd properties don't exist in IE until version 9, your IE8 will be using the range method which works fine ie IE6 ("the problem child") so I'm going to guess the issue is down to the implementation. Do you have this online to look at? Quote Link to comment https://forums.phpfreaks.com/topic/245522-ast-character-of-a-textarea-in-javascript/#findComment-1261437 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.