1

I have to write a code which will convert text input to my language (Bangla). For this, I detect every keystroke, if it falls within A-Z or 0-9 boundary I write corresponding code and finally I set the cursor to the specific position.

Now, I am having problem in <input type="textbox" /> type element because I get the cursor position correct, I write into correct position but the problem is:

Consider a case where you typed a character where the visible area in the textbox is for 10 character only. In this case, the desired output is: after I re-write my modified value into the textbox at 11th position inputbox should show position: 1 to 11 and cursor should be placed in 11th position.......But in my case, the visible area remains at 0 to 10th character position (i mean initially as it was)

I have tried every possible solution I found in stackoverflow but did not solved this problem. I tried this demo page most: http://demo.vishalon.net/getset.htm


The code can be found at:

http://jsbin.com/abexeq/3/edit

and demo:

http://jsbin.com/abexeq/3/


Please help me to write the writeFinalValue function at top of the HTML portion so that this problem is solved.

Code:

/// <summary>Write finalText to the text/input box</summary>
Keyboard.prototype.writeFinalValue = function (finalText, caretPosition) {

    var scrollTop = this.textInputSource.scrollTop;

    if (typeof this.textInputSource.selectionStart == "number" && typeof this.textInputSource.selectionEnd == "number") {
        // Non-IE browsers and IE 9
        this.textInputSource.value = finalText;

        this.textInputSource.value = this.textInputSource.value;
        //        // Move the caret
        //        this.textInputSource.selectionStart = caretPosition;
        //        this.textInputSource.selectionEnd = caretPosition;
    }
    else if (document.selection && document.selection.createRange) {
        // For IE up to version 8
        var selectionRange = document.selection.createRange();
        var textInputRange = this.textInputSource.createTextRange();
        var precedingRange = this.textInputSource.createTextRange();
        var bookmark = selectionRange.getBookmark();
        textInputRange.moveToBookmark(bookmark);
        precedingRange.setEndPoint("EndToStart", textInputRange);
        var start = precedingRange.text.length;
        var end = start + selectionRange.text.length;

        this.value = finalText;

        //        // Move the caret
        //        textInputRange = this.createTextRange();
        //        textInputRange.collapse(true);
        //        textInputRange.move("character", start - (this.textInputRange.value.slice(0, start).split("\r\n").length - 1));
        //        textInputRange.select();
    }

    this.textInputSource.focus();
    this.textInputSource.scrollTop = scrollTop;

    // move caret
    if (this.textInputSource.setSelectionRange) {
        this.textInputSource.setSelectionRange(caretPosition, caretPosition);
//        this.textInputSource.value = this.textInputSource.value;
    }
    else if (this.textInputSource.createTextRange) {
        var range = this.textInputSource.createTextRange();
        range.collapse(true);
        range.moveEnd('character', caretPosition);
        range.moveStart('character', caretPosition);
        range.select();
    }
}

Thanks.

N.B. I need suggestion for Firefox/Chrome mainly because Internet Explorer is not my concern.

seoul
  • 864
  • 1
  • 12
  • 32
  • 1
    Live links are a great *adjunct* to a question, but always post the relevant code *in the question* as well. Two reasons. 1. People shouldn't have to follow a link to help you. 2. StackOverflow is meant to be a resource not just for you now, but for others having a similar issue in the future. External links can get moved, modified, deleted, etc. By making sure the relevant code is in the question, we ensure that the question (and its answers) remain useful for a reasonable period of time. – T.J. Crowder Jul 06 '11 at 06:00
  • I've corrected the formatting for you, one of your paragraphs was near-unreadable because it was indented with four spaces. That means "this is code" to StackOverflow and so it's shown mono-spaced and not word-wrapped... Check out the **How to Format** box to the right when you're typing your question, and the preview area underneath it where you can see the results. – T.J. Crowder Jul 06 '11 at 06:02

1 Answers1

0

The most reliable cross-browser solution I've seen to set the focus/cursor at the end of a textbox so far, is the jQuery PutCursorAtEnd plugin. I would recommend you to go with this plugin to solve this issue.

Update

I've tried it out and it actually is scrolling to the end of the textbox if the text is overflowing. In your example (http://jsbin.com/unejup/6) it seems also to work, but only for the second click on the "Set position" button.

<script>
// http://plugins.jquery.com/project/PutCursorAtEnd
$(function () {
    $('#foo').putCursorAtEnd();
});
</script>

<input type="text" id="foo" value="abcdefghijklmnopqrstuvwxyz" />
Martin Buberl
  • 45,844
  • 25
  • 100
  • 144
  • Tried their code. it didn't work :-( The problem is until i re-write the inputbox content the view is in 1-11 position but as soon as i write something, the text is placed at 11th position but viewarea shows 0-9 position. It should be sliding.......From your link, i exactly implemented that: `this.setSelectionRange(len, len);`.....But no luck cause the cursor is placed there but the view area is not slided – seoul Jul 06 '11 at 06:53
  • I wasn't aware that it doesn't "scroll" to the end if there's overflowing text in the textbox. On this SO question/answer are a couple of other options, maybe it helps: http://stackoverflow.com/questions/511088/use-javascript-to-place-cursor-at-end-of-text-in-text-input-element – Martin Buberl Jul 06 '11 at 07:03
  • I think it is a fundamental design of browsers for which I am not getting my desired result. Check with this link: http://jsbin.com/unejup/6 , here type any position and press "set position".....Then position will be set (easily verifiable by pressing "get position") but the view area will not be shifted – seoul Jul 06 '11 at 07:06
  • Just another (untested) idea what you could try. What happens if you first set the focus to the end of the textbox and then trigger an event like keyup. – Martin Buberl Jul 06 '11 at 07:13
  • simulated this behaviour but no luck :-| – seoul Jul 06 '11 at 08:43
  • I tried it and updated my answer. Your example seems to only work for the second click, so I would look deeper into this behavior. It generally seems to work. Hope that helps. – Martin Buberl Jul 06 '11 at 09:59
  • okk...but in my pc it's not working even on 2/3/4th click :-( anyways, let it be as it is and cope with it. – seoul Jul 08 '11 at 11:07
  • Thanks for your efforts in answering also :-) – seoul Jul 08 '11 at 11:07