views:

104

answers:

3

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

How can I do this?

+3  A: 

This link should help you out:

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

Travis
+2  A: 

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.

Adam Bard
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/
WesleyJohnson
A: 

Are you looking to bold the entire text area or only a segment of text with in the text area?

Aizotu
either where the cursor is or what is currently selected.
mrblah
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 "<b></b>" work inside the value element.
Aizotu