hi i am trying to implement the delete and back space using javascript,the problem i am facing is whenever i hit the delete button it starts deleting from the end instead of the current cursor position and continues like backspace after that till the end. the code for delete is below:

case "btnDel": // ID of button
                    {
                        try
                        {
                            var temp="";
                            if (document.getElementById("txtSearch").value.length>0)
                            {
                                document.getElementById("txtSearch").value=document.getElementById("txtSearch").value.substring(0,document.getElementById("txtSearch").value.length-1);
                            }
                        }

code for bank space:

case "btnBackSpace": 
                    {
                        try
                        {
                            var temp="";
                            
                            if (document.getElementById("txtSearch").value.length>0)
                            {
                                document.getElementById("txtSearch").value=document.getElementById("txtSearch").value.substring(0,document.getElementById("txtSearch").value.length-1);
                            }

can anyone please help me to achieve my goal of implementing the back space and delete in the true sense!!

thank you!!

Er.. yeah. Use the sure-fire, well tested, unecessary to even implement behaviour of a standard input element.

If you MUST reimplement the behaviour of an input yourself, reimplement it entirely, and don't use the standard input as a base; in that way, you can easily control and query the cursor position yourself, and then you know where to start deleting from. Hard work? relatively.

If you MUST use the standard input and you MUST use your own code to do backspace/delete, you'll need to use crazy code like this:
http://weetbixthecat.com/blog/2006/getting-and-setting-the-cursor-position-in-an-input/
to get hold of the current position of the cursor. It's crazy because, in internet explorer, you need to calculate a content length offset from the beggining of the page to the beggining of the input, in order to work out exactly where the cursor is relative to the input itself. Then, you can use that offset to determine from where to start removing text.

Also remember that UNIX variants and VMS define those keys differently.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.