473,574 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert a string into the text of textarea. Caret? IE question

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...am12032001.asp
in the chapter "O Cursor, Where Art Thou".

Here is what happens (I use caretPos.text = "abc";):

1. If no text is selected - no problem, my "abc" got inserted say
after '2' AND I see a Caret (vertical bar) after it

2. But if - with the same code - I _select_ say "56" and want to
override it with "abc", then yes, "56" got replaced with "abc",
BUT there is no Caret symbol on screen, no vertical bar!
Is it solvable?

You can see the code working by typing say 0123456789, selecting
several digits and then clicking with a mouse on any
letter of a keyboard image:
http://RusWin.net/screen_e.htm

(in that specific mouse-driven mode a replacement is one symbol, but
in other modes it's not the case, the replacement could be a _string_)

The script piece is below.

--
Regards,
Paul Gorodyansky
"Cyrillic (Russian): instructions for Windows and Internet":
http://RusWin.net
Russian On-screen Keyboard: http://Kbd.RusWin.net

========== Script piece

// elem is a TEXTAREA object
function getCaretPos(ele m, newString)
{

if ( elem.isTextEdit )
{
if ( !elem.caretPos )
saveCaret(elem) ;

var caretPos = elem.caretPos;
caretPos.text = newString;
}
}

function saveCaret(elem)
{
if ( elem.isTextEdit )
elem.caretPos = document.select ion.createRange ();
}
// HTML piece:
<textarea name='message' ONSELECT='saveC aret(this)'
ONCLICK='saveCa ret(this)' ONKEYUP='saveCa ret(this)'
</textarea>
Jul 23 '05 #1
7 30319
Example similar to MS' one is here -
http://www.faqts.com/knowledge_base/...d/1052/fid/130
and I used it, too - to keep track of cursor position....

But that example shows the same issue - no Caret symbol (bar) after
the operation.

--
Regards,
Paul
Jul 23 '05 #2
/Paul Gorodyansky/:
Example similar to MS' one is here -
http://www.faqts.com/knowledge_base/...d/1052/fid/130
and I used it, too - to keep track of cursor position....

But that example shows the same issue - no Caret symbol (bar) after
the operation.


You're clearly taking the focus away from the text area pushing the
button - what's the problem here?

How do you intend to activate the insertion? If you're going to do
it the same way - user pushing a button, and you still want
returning the focus to the area you could add:

textEl.focus();

at the end of the 'insertAtCaret( )' function, for example. Focus
issues could be tricky and it greatly depends on your functional
requirements - what should happen when the text area doesn't have
the focus and one pushes the button?

--
Stanimir
Jul 23 '05 #3


Paul Gorodyansky wrote:
Example similar to MS' one is here -
http://www.faqts.com/knowledge_base/...d/1052/fid/130
and I used it, too - to keep track of cursor position....

But that example shows the same issue - no Caret symbol (bar) after
the operation.


Well it is only about insertion at the caret, if you want to have the
caret in there then I would try with calling

function insertAtCaret (textEl, text) {
if (textEl.createT extRange && textEl.caretPos ) {
var caretPos = textEl.caretPos ;
caretPos.text =
caretPos.text.c harAt(caretPos. text.length - 1) == ' ' ?
text + ' ' : text;
caretPos.select ();
}

If that doesn't help then at least for IE 5.5/6 on Windows MS has
introduced an attribute named unselectable for buttons so that you can
click them without taking focus away/changing a selection thus if you use
<input type="button"
unselectable="o n"
with your buttons then there should be a problem with the button
interfering with the selection in a text control.
Docs are here:
<http://msdn.microsoft. com/library/default.asp?url =/workshop/author/dhtml/reference/properties/unselectable.as p>

See also
<http://www.faqts.com/knowledge_base/view.phtml/aid/13562/fid/130>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #4
Stanimir Stamenkov wrote:

/Paul Gorodyansky/:
Example similar to MS' one is here -
http://www.faqts.com/knowledge_base/...d/1052/fid/130
and I used it, too - to keep track of cursor position....

But that example shows the same issue - no Caret symbol (bar) after
the operation.


You're clearly taking the focus away from the text area pushing the
button - what's the problem here?


No, it's just an example - in my case often there is NO button, no
"taking focus away", for example, in "To Latin" mode:

a) No selection
- I place cursor in the middle of the text - see Caret bar
- I press a letter on my physical keyboard (say, Cyrillic letter
that looks like mirrored 'R')
- in the script, I replace that letter with 2 latin letters 'ya'
and do caretPos.text =
- on screen, 'ya' is inserted and Caret (bar) is after that insertion

b) When I selected some text and want to replace it:
- I place cursor in the middle of the text - see Caret bar
- I select several symbols
then - same as in (a):
- I press a letter on my physical keyboard (say, Cyrillic letter
that looks like mirrored 'R')
- in the script, I replace that letter with 2 latin letters 'ya'
and do caretPos.text =
- on screen, the selected tex is replaced with 'ya' BUT there is
*no* Caret (bar) anymore.
Let me try Martin's function.
--
Regards,
Paul Gorodyansky
"Cyrillic (Russian): instructions for Windows and Internet":
http://RusWin.net
Russian On-screen Keyboard: http://Kbd.RusWin.net
Jul 23 '05 #5
Thanks, Martin,

Yes, it solves the problem - I mean adding caretPos.select ();
after assigning the new text - now even when I select say 3 symbols
and programmaticall y replace it with my string, I see Caret (bar) after
my string!

It's really useful to have a Caret (bar) visible - may be a user
wants to insert more - and now s/he sees _where_.

--
Regards,
Paul Gorodyansky
"Cyrillic (Russian): instructions for Windows and Internet":
http://RusWin.net
Russian On-screen Keyboard: http://Kbd.RusWin.net
Jul 23 '05 #6
Hi,

I added 'default' method for browsers that, unlike IE and Mozilla,
do not let me - using script - position a caret and insert/replace text
there (Opera, Safari, etc.), so it's just got added to the very end of
the text in TEXTAREA:

textControl.val ue += newString;

So it works fine say in Opera but bad thing is that Caret (bar) is not
moving (obviously).

Is it possible - in Opera - to position a Caret (bar) at the end of the
text?

--
Regards,
Paul Gorodyansky
"Cyrillic (Russian): instructions for Windows and Internet":
http://RusWin.net
Russian On-screen Keyboard: http://Kbd.RusWin.net
Jul 23 '05 #7


Paul Gorodyansky wrote:

Is it possible - in Opera - to position a Caret (bar) at the end of the
text?


I don't think that is possible in a HTML control in Opera currently, at
least there is no API. Some browsers when you set control.value however
move the caret, some to the beginning, some to the end of text in the
text control.
And there are Java text controls you could embed with a Java applet
which probably then allow you more control over the caret.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #8

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

Similar topics

5
3980
by: Zbigniew Braniecki | last post by:
Hello. I'm looking for any way to grab caret position in textarea (start, end - if selection) in Internet Explorer. Is it at all possible? Also any idea for doing it in Opera, KHTML would be appreciated :) Greetings Gandalf
4
19355
by: Ben R. | last post by:
Hi there, I'm designing a winforms app in VB.NET 2.0 and have a textbox control and also some toolbar buttons. I want to have it such that when the user clicks a toolbar button, some predefined text - such as the date - is inserted into the textbox at the point where the caret is. (It's assumed that the user will be typing and then click...
1
5013
by: solomon_13000 | last post by:
connection.asp: <% Sub RunQueryString (pSQL,parms) on error resume next Set conn = Server.CreateObject("ADODB.Connection") conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/db/VideoBlog.mdb") & ";" Set cmd = Server.CreateObject("ADODB.command") With cmd
2
6505
by: rjonasz | last post by:
Hey Everyone, I'm trying to scroll a textarea to the position of the caret which is below the visible area of the textarea in firefox. I have code which works in IE but the code I use for firefox creates an elastic band effect. The textarea quickly scrolls down then bounces up to the top, no matter what I try. Has anyone run into the...
2
2131
by: kisalabs | last post by:
I have been trying to insert a new record into an access database and cannot get any of the scripts to work.. I can access the db and pull up/display records but cannot get one added. Please help. (info to be inserted comes from form) Inserting Records : <form action="a.asp" method="post"> FirstName : <input type="text"...
8
5113
by: =?Utf-8?B?QkxVRVNUQVI=?= | last post by:
HELLO, I AM A NEW BII TO ASP,AND I NEED HELP ON THIS ISSUE. I HAVE A .ASP FORM WHICH IS ALREADY BUILT WITH 5 QUESTIONS AND 5 TEXTAREAS RESPECTIVELY.THESE 5 QUESTIONS ARE NOT FIX,THERE WILL BE MORE ADDITION. ITS BASICALLY A SURVEY FORM. I HAVE CREATED A TABLE WITH 3 FIELDS :QUESTION ID,RESPONSE IN TEXTAREA, AND DATE.
3
8390
by: ReGenesis0 | last post by:
Is it possible to determine the caret position, in terms of x/y pixels, within a textarea? (I want to have a suggestion box pop up under where you're typing... so i need to determine where you are typing.) -Derik (OTOH, finding the Caret position-- between what characters the cursor is positioned-- uses the function below. I include it...
58
8033
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also the parts section an i seem to be having trouble. When i try to insert into the parts section i get the error Invalid character value for cast...
0
7720
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...
0
8230
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...
1
7810
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...
1
5618
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5299
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...
0
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2240
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
1
1337
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1056
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...

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.