Jump to content

onclick send text help


phpmo

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.