Hi,

I have a few buttons to manipulate selected text in textarea but cannot work out how to do it. I mean I tried some examples on web but they are not really cross-browser compatible. If one work then another wont etc.

Can anyone help me modify what I have below?

Thanks in advance

    <script text="text/javascript">  
    function insertAtCursor(myField, request)
    {
        var selected_text = ..............;

        if (request == 'a')
        {
            var user_input = prompt('Please enter the link:', 'http://');
            var add = '<a href="' + user_input + '">' + selected_text + '</a>';
        }
        else
        {
            var add = '<' + request + '>' + selected_text + '</' + request + '>';
        }

        Update_selected_text_within_textarea_here = add;
    }
    </script>


    <form name="myform">

        <input type="button" value="link" onclick="insertAtCursor(document.myform.textarea_content, 'a');" /> 
        <input type="button" value="bold" onclick="insertAtCursor(document.myform.textarea_content, 'b');" /> 
        <input type="button" value="italic" onclick="insertAtCursor(document.myform.textarea_content, 'i');" /> 
        <input type="button" value="under" onclick="insertAtCursor(document.myform.textarea_content, 'u');" />

        <br />

        <textarea id="mytextarea" name="textarea_content"></textarea>

    </form>

If the issue is compatibility, has you considered implementing this via jQuery instead?

No but I was thinking about it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.