474,044 Members | 3,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to delete a character at the caret? Extend selection?

I have a text field in a form. I want the user to be able to click
a DELETE button and have the character at the cursor position deleted.
This would be just as if the user had pressed the Back Space key on the
keyboard... But this is for a kiosk application with no keyboard :-(
so I need to program a delete button.

I have code that lets me insert text at the cursor position.
And I can move the ABSOLUTE cursor position from the start and end.
For example, if my text field looks like this (With | representing the caret):
ABCDEF|GHIJKL
....and I call moveStart('char acter', 1) I get this:
A|BCDEFGHIJKL

I was hoping that I could move the selection RELATIVE to the current position:
var r = my_field.create TextRange();
r.moveStart('ch aracter', -1); // This doesn't work...
r.select();
r.text = "";
Or if I could at least get the cursor or selection coordinates:
var r = my_field.create TextRange();
var LEFT = r.start; // This doesn't work
r.moveStart('ch aracter', LEFT-1);
r.select();
r.text = "";
Mozilla lets you get the selectionStart, but I can't figure out how to
do this for IE. Is there anything like selectionStart in IE?

Yours,
Noah
Jul 20 '05 #1
3 6688
Noah wrote:
I have a text field in a form. I want the user to be able to click
a DELETE button and have the character at the cursor position deleted.
This would be just as if the user had pressed the Back Space key on the
keyboard... But this is for a kiosk application with no keyboard :-(
so I need to program a delete button.
I have code that lets me insert text at the cursor position. [] I was hoping that I could move the selection RELATIVE to the current position:
var r = my_field.create TextRange();
have you tried

var r=document.sele ction.createRan ge()

instead of that?
r.moveStart('ch aracter', -1); // This doesn't work...
r.select();
r.text = "";


There are many ways achieving what you want.
Probably the dumbest using the inverse logic of the direction of
deletion would be:

get selection,
move one character backwards r.move('charact er', -1)
and then execCommand('De lete')
HTH

--
marekmand.kuubi k.ee | Marek Mand
Jul 20 '05 #2
Marek Mand <ca********@mai l.ee> wrote in message news:<c2******* ******@ID-27552.news.uni-berlin.de>...
have you tried

var r=document.sele ction.createRan ge()


Ah! That was the trick. I get createRange and createTextRange mixed up.
This should also work for moving the cursor left and right; although,
I'm not sure how I will get cursor up and cursor down to work.

The full code for Back Space follows.
There is extra logic because the BackSpace key behaves differently if
there is already a selected range. The only drawback I found
is that the caret is hidden after this. The field still has focus
and any new typing will reveal the caret, but the caret is hidden until then.
// This emulates the Back Space (BkSP) key in JavaScript for IE.
var r = document.select ion.createRange ();
if (r.text.length == 0) { // Select one character to the left of caret.
r.move('charact er', -1);
r.moveEnd ('character', 1);
}
r.execCommand ('Delete');
r.execCommand ('Unselect');
Note: if you want Delete (del) key behavior instead of BkSpc then don't
execute the r.move('charact er',-1) line of code.

Now I just have to figure out how to do this in Mozilla :-P
Will there ever be a unified DOM?

Yours,
Noah
Jul 20 '05 #3
Noah wrote:
Marek Mand <ca********@mai l.ee> wrote in message news:<c2******* ******@ID-27552.news.uni-berlin.de>...
have you tried
var r=document.sele ction.createRan ge() Ah! That was the trick. I get createRange and createTextRange mixed up.
This should also work for moving the cursor left and right; although,
I'm not sure how I will get cursor up and cursor down to work.

up? down? mm, aha you have a textarea...
this will be quite tricky, as IHMO move('caracter' ) is broken there.
But that is only my personal opinion.

The full code for Back Space follows.
As you were so nice and posted back so the others can learn from it,
which is quite rare nowadays
There is extra logic because the BackSpace key behaves differently if
there is already a selected range. The only drawback I found
is that the caret is hidden after this. The field still has focus
and any new typing will reveal the caret, but the caret is hidden until then. [code]

then I take and answer also this.
The aswer for your problem probably is:

r.select();

after finishing the programmatic editing operations.
If it doesnt work, then do tell me.

// This emulates the Back Space (BkSP) key in JavaScript for IE.
var r = document.select ion.createRange ();
if (r.text.length == 0) { // Select one character to the left of caret.
r.move('charact er', -1);
r.moveEnd ('character', 1);
have you tried now a single moveStart('char acter', -1);
instead of those two statements?
}
r.execCommand ('Delete');
r.execCommand ('Unselect');
Note: if you want Delete (del) key behavior instead of BkSpc then don't
execute the r.move('charact er',-1) line of code. Now I just have to figure out how to do this in Mozilla :-P Will there ever be a unified DOM?


Why are you so mean and demand for salary cuts, what on earth do you
have on your mind? ;D

p.s.
I am not an active reader of cljs for some years now, so do tell if you
are done with this thread.

--
marekmand
EU, Estonia, Tallinn | marekmand.kuubi k.ee
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5714
by: torch | last post by:
I have trawled the internet for a solution to this one . All I need is the caret position as a number. I have seen quite a few text insertion functions that do something like this : textarea.caretPos = document.selection.createRange().duplicate() and then operate on the caretPos object - but all these solutions seem to work by creating a selected area in the text and then replacing it - I just want the caret position as a number.
7
30433
by: Paul Gorodyansky | last post by:
Hi, Say I have a text in my TEXTAREA box - 01234567890 I want - using script - insert say "abc" in the middle. Works almost OK in Internet Explorer (with one problem) based on their example at http://msdn.microsoft.com/library/en-us/dnwebteam/html/webteam12032001.asp in the chapter "O Cursor, Where Art Thou".
4
1857
by: kimimaro | last post by:
Thank you for your help I think I know what the problem is. Firstly the add_record cannot read the record.txt or something if the record.txt (in which I used it to store the details of each record) is present in the same directory. When it prompt enter your selection from the menu, it will just stuck there so I cant even create a new entry and thus I cant check the modify function in my program. If it's not present, it will create a...
3
4031
by: ozbear | last post by:
The title says it all. Since a Richedit control doesn't understand a backspace character ('\b', or 0x08) when I receive one I need to programmatically delete the last character from the Richedit control. This is a WinForms application, not a webform. I have tried... re.SelectionLength = 1; re.SelectionStart = re.Textlength-1; re.SelectionText = ""; // empty string
2
2916
by: randy | last post by:
Hi, I'm trying to set up a richtextbox to behave like the text area in a web browser, i.e. no caret, and a cursor that changes depending on what it's hovering over. I've been setting the cursor by testing the "Selection" properties of the text the cursor is over. This requires setting the SelectionStart property, which causes the caret to move. Ugh!
7
1261
by: One Handed Man [ OHM# ] | last post by:
OK, here is a mildy interesting one. What is the cleanest way to do the following. Every time a keyPress event occurs, I wan to check for the quote ( " ) and subsitute it for the apostrophe ( ' ). Detecting this is easy, but what is the cleanest way to subsitute that character in the current cursor position in the textbox. Its a pity that you cannot alter the KeyValue but all those properties are readonly. The reason I'm asking this is...
0
2583
Jezternz
by: Jezternz | last post by:
Okay, so a while back I managed to retrieve it correctly. In this thread: http://bytes.com/topic/javascript/answers/786211-retrieving-caret-start-end-values#post3127765 However I have found that in (W3C compliant browsers) this does not work correctly inside a contentEditable division. With a couple of modifications, I can at least get the caret position, on a per line basis. var findCaretPosition = function(obj){ var startpos = 0;...
0
10546
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11602
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
12022
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7868
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
5416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.