Thursday, April 21, 2011

how to add a simple button that will inject a tag into a textarea?

I want to create a simple button that will inject a [b][/b] into a textarea.

How can I do this?

From stackoverflow
  • This link should help you out:

    http://www.webmasterworld.com/forum91/4686.htm

  • Your question is tagged jQuery, so I assume you're using it:

    HTML:

    <button id="mybutton>Bold</button>
    <textarea id="mytextarea"></textarea>
    

    JS:

    $('mybutton').click(function(){
        $('#mytextarea').append('[b][/b]');
    });
    

    Of course, a real BBCode implementation with the ability to surround highlighted text with [b] tags, and/or append [b] upon the first click of the button and [/b] upon the second, is another problem entirely.

    WesleyJohnson : You can check out this link on solutions for getting selected text from a textarea. http://corpocrat.com/2008/08/10/how-to-get-selected-value-in-textarea-using-javascript/
  • Are you looking to bold the entire text area or only a segment of text with in the text area?

    mrblah : either where the cursor is or what is currently selected.
    Aizotu : Well with a TextArea, I think you're going to have a problem as I've yet to find a way to get html tags like "" work inside the value element.

0 comments:

Post a Comment