Jump to content

Javascript Something gone wrong


Dragon707

Recommended Posts

Hello all,

 

I just have a java script that i use for my texteditor but for insert the UBB codes go something wrong.

 

When i select some text and i use UBBCode('b') than is the output wrong al is gone but the selected isnt gone.

 

Script:

<script language="JavaScript">
    function UBBCode(UBB){
        var SELECT=(document.rte.tekst.value).substring(document.rte.tekst.selectionStart,document.rte.tekst.selectionEnd);
        var edit='['+UBB+']'+SELECT+'[/'+UBB+']';
        document.rte.tekst.value=edit;
        document.rte.tekst.focus();
    return;
    }
</script>

 

I hope that somebody can help me, PS: Sorry for my bad english. I am dutch

Link to comment
Share on other sites

I would also pass the field ID to the function instead of hard codsing the field intot he function to make it more flexible. The script also needs to get the text before and after the selected text to do what you want. This corrects that - however selectionStart and selectionEnd do not work in IE.

 

<html>
<head>
<script type="text/javascript">

    function UBBCode(fieldID, UBB)
    {
        fieldObj = document.getElementById(fieldID);
        var BEFORE = (fieldObj.value).substring(0, fieldObj.selectionStart);
        var SELECT = (fieldObj.value).substring(fieldObj.selectionStart, fieldObj.selectionEnd);
        var AFTER  = (fieldObj.value).substring(fieldObj.selectionEnd);
        fieldObj.value = BEFORE + '['+UBB+']' + SELECT + '[/'+UBB+']' + AFTER;
        fieldObj.focus();
        return;
    }

</script>
</head>
<body>

<textarea id="tekst"></textarea>
<button onclick="UBBCode('tekst', 'B')";>TEST</button>
</body>
</html>

Link to comment
Share on other sites

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.