Click to See Complete Forum and Search --> : Inserting to textarea at cursor


pyro
01-16-2003, 12:12 PM
Does anyone know if there is a way to insert text to a textarea where the cursor is? I can't just append to the end, I need to somehow append to the middle (at the cursor). Any ideas??

swon
01-16-2003, 02:12 PM
For which browser should it works?

pyro
01-16-2003, 02:14 PM
All that's needed is IE 5.5+

swon
01-16-2003, 02:39 PM
Can it also be a div? (when you use a form the value of the div could saved to a hidden and then send)

pyro
01-16-2003, 03:11 PM
Ok, what I'm doing is very complex. I'm making a HTML editor in HTML, JavaScript and PHP. I'm switching back and forth between an editable IFrame (contenteditable) and a textarea. The textarea is a "code view" if you will, and the IFrame is the "design view". So, I'm trying to be able to add tables. What I need to do if find where their cursor is in the IFrame, mark the spot, move over to the textarea and generate code depending on the choices they made for the table. (number of rows, border, etc). If that doesn't make sence, I'll send you an email with my code, (I'd rather not post it all).

khalidali63
01-16-2003, 03:40 PM
What is ist you want ot do once you know the center or the mouse location.?

Khalid

pyro
01-16-2003, 03:45 PM
Basically what I want to do is insert the code for the table there. But I have this problem (as well as getting the cursor location in the first place)... I can't just write it to the IFrame, I need to write it to the text area (code view) because the IFrame would turn <head> to &lt;table&gt;, because like I said, it is a design view, not code view. So, what I need to do is mark the cursor location, move to the textarea, and write the code at the cursor location. I hope that makes sence.

swon
01-16-2003, 03:49 PM
Ok, look at this code, it needs a page that you can defined where you put your forms, links or whatever, which is called by the function insert(what), the return for this showModalDialog-Window inserts the newcontent where the focus on the iframe is, hope that's it:

<script language="JavaScript">
<!--
function insert(what)
{
strPage = "insert.html";
strAttr = "status:no;dialogWidth:420px;dialogHeight:230px;help:no";

html = showModalDialog(strPage, "foo", strAttr);
if (html) {
iEdit.focus();
var sel = document.selection.createRange();
sel.pasteHTML(html);
}
}

function Init() {
iEdit.document.designMode = 'On';
}
//-->
</script>

</head>
<body onLoad="Init()">
<form name=test action="" method="" target="">
<iframe id=iEdit></iframe>
<button onClick="insert('text');">Insert Text</button>
</form>
</body>
</html>

pyro
01-16-2003, 03:58 PM
swon, either I'm using it wrong or it doesn't work. What it does is just opens a window with the text I specified in insert.html. It doesn't add it to the iFrame, much less at the cursor. Maybe I'm doing something wrong?

khalidali63
01-16-2003, 03:59 PM
Pyro try the link below and see if that is anything you wanted.
http://68.145.35.86/temp/FindCursorPosionONaTextArea.html

swon
01-16-2003, 04:05 PM
Sorry my fault, forgot to put this in the insert.html:

<script language="JavaScript">
<!--
function doIt(){
val = document.forms[0].texts.value;
window.returnValue = val;
window.close();
}
//-->
</script>

pyro
01-16-2003, 04:05 PM
No that wasn't what I was looking for, Kahlid, thanks for the effort, though. I need to be able to dynamically add text where the keyboard cursor is, not the mouse cursor. Sorry. Also, it doesn't have to work in NS, as other things I'm using doesn't work... execCommand, etc.

swon
01-16-2003, 04:11 PM
call the function doIt() above your last post width your link or button or whatever!

pyro
01-16-2003, 04:14 PM
Thanks swon, it's working. Now I just need to figure out if there is a way to move the cursor location over to the code view...I can probably think of somethin, even if it is just writing some temporary text, spliting it there, and the adding the table.... hmmm...

swon
01-16-2003, 04:19 PM
Why do you not make the codeview in the Iframe? I think thats easier then to beam it!

pyro
01-16-2003, 06:48 PM
It's because the IFrame changes code like <b> to &lt;b&gt;. So, the way around that was to make the code view a textarea. :)

zwomble
02-04-2003, 03:29 AM
if you are still interested in an editor with code view write back and i will send it to you...

pyro
02-04-2003, 07:02 AM
The only reason I'd be interested in your editor would be for research. I'm making my own, not just looking for one to use. :)

zwomble
02-04-2003, 12:19 PM
i will send the url/code on friday because i'm on holiday...yeah! ;-)
cu Zwomble

pyro
02-04-2003, 12:31 PM
Alright. Have fun, and hopefully I can learn something from your code. :)