473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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('character', 1) I get this:
A|BCDEFGHIJKL

I was hoping that I could move the selection RELATIVE to the current position:
var r = my_field.createTextRange();
r.moveStart('character', -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.createTextRange();
var LEFT = r.start; // This doesn't work
r.moveStart('character', 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 6639
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.createTextRange();
have you tried

var r=document.selection.createRange()

instead of that?
r.moveStart('character', -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('character', -1)
and then execCommand('Delete')
HTH

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

var r=document.selection.createRange()


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.selection.createRange();
if (r.text.length == 0) { // Select one character to the left of caret.
r.move('character', -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('character',-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********@mail.ee> wrote in message news:<c2*************@ID-27552.news.uni-berlin.de>...
have you tried
var r=document.selection.createRange() 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.selection.createRange();
if (r.text.length == 0) { // Select one character to the left of caret.
r.move('character', -1);
r.moveEnd ('character', 1);
have you tried now a single moveStart('character', -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('character',-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.kuubik.ee
Jul 20 '05 #4

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

Similar topics

4
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 : ...
7
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...
4
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)...
3
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...
2
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...
7
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 ( '...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.