javascriptxulunobtrusive-javascript

How to check dropped text position inside a textbox?


I have a textbox with string, and want to check that user dropped text between textbox string or at end of textbox string. Is there is any Javascript function to do this task? I tried createTextRange() and setSelectionRange() but nothing works.


Solution

  • HTML

    <input type="text" id="myTextInput" value="test" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
    

    JS

    var myInput = document.getElementById('myTextInput');
    var defaultValue = myInput.value;
    
    myInput.addEventListener("input", function () { 
    
        var index = myInput.value.indexOf(defaultValue, 0);
        if(index == 0)
            alert('The text has been added at the end of the original string');
        else if(index == -1)
            alert('The text has been added inside the original string');
        else
            alert('The text has been added in the front of the original string');
    
    }, false);
    

    DEMO