Jump to content

Recommended Posts

<img name="7" img src="../images/italic.gif" border="0" alt="Italic"onClick="sendText(document.form2.rep, '[i] [/i]')">

 

I'm using the above code to insert various text fields as bbcode so that I can transfer it to html once viewed.

 

This works but inserts the text at the end of the text. So if text already exists and I try to type above the old text, when you click them it still inserts it below the old text and not where my cursor is.

 

Any thoughts on a way to make the text go to the cursor rather than the end of the textbox?

Link to comment
https://forums.phpfreaks.com/topic/181686-onclick-send-text-help/
Share on other sites

From http://www.scottklarr.com/topic/425/how-to-insert-text-into-a-textarea-where-the-cursor-is/

 

function insertAtCaret(areaId,text) {
    var txtarea = document.getElementById(areaId);
    var scrollPos = txtarea.scrollTop;
    var strPos = 0;
    var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? 
        "ff" : (document.selection ? "ie" : false ) );
    if (br == "ie") { 
        txtarea.focus();
        var range = document.selection.createRange();
        range.moveStart ('character', -txtarea.value.length);
        strPos = range.text.length;
    }
    else if (br == "ff") strPos = txtarea.selectionStart;

    var front = (txtarea.value).substring(0,strPos);  
    var back = (txtarea.value).substring(strPos,txtarea.value.length); 
    txtarea.value=front+text+back;
    strPos = strPos + text.length;
    if (br == "ie") { 
        txtarea.focus();
        var range = document.selection.createRange();
        range.moveStart ('character', -txtarea.value.length);
        range.moveStart ('character', strPos);
        range.moveEnd ('character', 0);
        range.select();
    }
    else if (br == "ff") {
        txtarea.selectionStart = strPos;
        txtarea.selectionEnd = strPos;
        txtarea.focus();
    }
    txtarea.scrollTop = scrollPos;
}

Usage:

<textarea id="textareaid"></textarea>
<a href="#" onclick="insertAtCaret('textareaid','text to insert');return false;">

That worked great.

 

What if I want to load this in another window and then send it to a different page.

 

For example how on the smilies here you can click [more] and it will open them up in a different page but it still goes to this text box.

 

Also, any suggestions on how that select box works?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.