Jump to content

Place cursor at end of line in editable div


The Little Guy

Recommended Posts

So, I am using this on a keypress event, the problem I am having is with range.setStart

 

What I am trying to do is place the cursor at the end of the line when the user presses a key.

 

$(document).ready(function(){
    $("#q").keypress(function(e){
        var txt = $(this).text();
        var key = String.fromCharCode(e.keyCode);
        txt = txt + key;
        var last = txt.lastIndexOf("in ");
        if(last == -1){
            last = txt.length + 1;
        }
        var str = txt.substr(0, last);
        $(this).html("<b>"+str+"</b>"+txt.substr(last));

        var el = document.getElementById("q");
        var range = document.createRange();
        var sel = window.getSelection();
        var length = $(this).text().length;
        range.setStart(el, length);
        range.collapse(true);
        sel.removeAllRanges();
        sel.addRange(range);
        el.focus();

        return false;
    });
});

 

In chrome I am getting this error

Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1

 

The code works on the very first key press, so if i type "h" the cursor gets placed after the "h" but on the second key press I get the above error in chrome.

 

Any suggestions?

Link to comment
Share on other sites

good point, I think I have that covered now. But how do I fix the error?

 

var caretPos = 0;
$(document).ready(function(){
    $("#q").keypress(function(e){
        var txt = $(this).text();
        var key = String.fromCharCode(e.keyCode);
        txt = (txt + key).toString();
        var last = txt.lastIndexOf(" in ");
        if(last == -1){
            last = txt.length + 1;
        }
        var str = txt.substr(0, last);
        $(this).html("<b>"+str+"</b>"+txt.substr(last));

        console.log(caretPos)

        var el = document.getElementById("q");
        var range = document.createRange();
        var sel = window.getSelection();
        range.setStart(el, caretPos);
        range.collapse(true);
        sel.removeAllRanges();
        sel.addRange(range);
        el.focus();

        return false;
    }).keyup(function(){
        var sel = window.getSelection();
        range = sel.getRangeAt(0);
        caretPos = range.endOffset;
    });
});

Link to comment
Share on other sites

<style>
#q{
    font-size: 17px;
    color: #555555;
    border: 0;
    border-radius: 5px;
    box-shadow: inset 1px 1px 5px rgba(0,0,0,.7);
    padding: 8px;
    padding-right: 20px;
    width: 400px;
    height: 40px;
    background-color: #ffffff;
    overflow: hidden;
    white-space: nowrap;
}
</style>
<div name="q" id="q" contenteditable="true"></div>

 

if you remove:

 

var el = document.getElementById("q");
        var range = document.createRange();
        var sel = window.getSelection();
        range.setStart(el, caretPos);
        range.collapse(true);
        sel.removeAllRanges();
        sel.addRange(range);
        el.focus();

 

you can see how it is supposed to work, only the cursor stays at the beginning of the line

Link to comment
Share on other sites

I got it!

 

I used this library:

http://code.google.com/p/rangy/

 

and this code:

 

function format_type(){
   // Save the selection
   var savedSel = rangy.saveSelection();

   // Do your formatting here
   format();

   // Restore the original selection
   rangy.restoreSelection(savedSel);
}
function format(){
   var el = document.getElementById('q');
   var text = el.innerHTML.replace(/<b>|<\/b>/gm, "");
   text = text.replace(/^(.+)( in.+)$/igm, "<b>$1</b>$2");
   if(!text.match(/ in /i)){
       text = text.replace(/(.+)/, "<b>$1</b>");
   }
   el.innerHTML = text;
}
$(document).ready(function(){
   format();
   $("#q").keypress(function(e){
       if(e.keyCode == 13){
           return false;
       }
   }).keyup(function(e){
       if(e.keyCode == 13){
           window.location = "/search.php?q="+escape($(this).text())+"&ac=search";
       }
       format_type();
   });
});

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.