Jump to content

ast character of a textarea in javascript


php_begins

Recommended Posts

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);

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>

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.

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)

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.