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
https://forums.phpfreaks.com/topic/134087-javascript-something-gone-wrong/
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>

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.