Jump to content

Need help with BBCode...


gtal3x

Recommended Posts

Hi evryone

i have got a function thats adds a bbcode to textarea once an images been clicked, the function works fine apart from 1 thing. When for example you press the B image it calculates (if am right) the cursors position so it will always print the coursor between the B tags or the text if its been selected before. So everything is great, however if i reload the page and then just click at B

WITHOUT 1st clicking at textarea then it will print first the b tags and then the cursor! (so it acctualy doest remember the cursor position because simply i dident click the text area!), Any idea how to fix it so the cursor is always in the middle even if i dident click on textarea?

 

That was my main question, the second less important question is (answer this only if you really got nothin else to do coz i dont wonna bather u too much  ;D)

is it possible when someone clicks ex. the B image, if he dident select any text, instead of printing both b tags (the start one end the end one), just print the start one, and then when you press again the b tag , print the end one

 

Here is my code:

 

function storeCaret(text)
{
    // Only bother if it will be useful.
    if (typeof(text.createTextRange) != "undefined")
        text.caretPos = document.selection.createRange().duplicate();
}

<script>
function AddCode(opentag, closetag)
{
    // Can a text range be created?
    if (typeof(document.comment.comment.caretPos) != "undefined" && document.comment.comment.createTextRange)
    {
        var caretPos = document.comment.comment.caretPos, temp_length = caretPos.text.length;

        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? opentag + caretPos.text + closetag + ' ' : opentag + caretPos.text + closetag;

        if (temp_length == 0)
        {
            caretPos.moveStart("character", -closetag.length);
            caretPos.moveEnd("character", -closetag.length);
            caretPos.select();
        }
        else
            document.comment.comment.focus(caretPos);
    }
    // Mozilla text range wrap.
    else if (typeof(document.comment.comment.selectionStart) != "undefined")
    {
        var begin = document.comment.comment.value.substr(0, document.comment.comment.selectionStart);
        var selection = document.comment.comment.value.substr(document.comment.comment.selectionStart, document.comment.comment.selectionEnd - document.comment.comment.selectionStart);
        var end = document.comment.comment.value.substr(document.comment.comment.selectionEnd);
        var newCursorPos = document.comment.comment.selectionStart;
        var scrollPos = document.comment.comment.scrollTop;

        document.comment.comment.value = begin + opentag + selection + closetag + end;

        if (document.comment.comment.setSelectionRange)
        {
            if (selection.length == 0)
                document.comment.comment.setSelectionRange(newCursorPos + opentag.length, newCursorPos + opentag.length);
            else
                document.comment.comment.setSelectionRange(newCursorPos, newCursorPos + opentag.length + selection.length + closetag.length);
            document.comment.comment.focus();
        }
        document.comment.comment.scrollTop = scrollPos;
    }
    // Just put them on the end, then.
    else
    {
        document.comment.comment.value += opentag + closetag;
        document.comment.comment.focus(document.comment.comment.value.length - 1);
    }
}
</script>

 

Thanks in advance for any help! i guess the solution to the 1st question should be easy...

 

 

Link to comment
https://forums.phpfreaks.com/topic/79009-need-help-with-bbcode/
Share on other sites

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.