Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<html>
    <body>
        <div>
            <textarea cols="20" id="Story" name="Story" rows="2" style="width:700px;height:300px">
                    this is a an example on how to insert text inside an input field like this one, precisly at the position of the cursor.
                    to try this out, place the cursor at any point inside, and hit the button "let the magic begin!"
            </textarea>
        </div>
        <input type="button" value="let the magic begin!" onclick='javascript:DoTheWork()' />
    </body>
    
</html>
 
function DoTheWork()
{
    var textarea = document.getElementById("Story");
    var myTextToBeInserted = "**MAGIC**";
    insertAtCursor(textarea, myTextToBeInserted);
}
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;
    }
}
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers