function makeBold(elem_id) {
        var start_text = '<b>';
        var end_text = '</b>';
        if (document.layers || window.opera)
                return;
        else if (document.all) {
            selected_text = document.selection.createRange();
            new_text = "<b>" + selected_text.text + "</b>";
            selected_text.text = new_text;
        }
        else if (document.getSelection) {
                elem = document.getElementById(elem_id);
                start_pos = elem.selectionStart;
                end_pos = elem.selectionEnd + start_text.length;
                elem.value = elem.value.slice(0,start_pos) + start_text + 
elem.value.slice(start_pos);
                elem.value = elem.value.slice(0,end_pos) + end_text + 
elem.value.slice(end_pos);

                
        }
}

<form>
        <textarea id="tfield" id="tfield">hello</textarea><br>
        <input type="Text" id="tfield_input" value="world"/>
        <input type="button" value="make bold" onclick="makeBold('tfield')"/>
</form>

Should work in IE4+(pc) and Gecko. I'd do this a bit differently if
you wanted to extend it beyond just bold tags though. Just replace
tfield with tfield_input in the onclick for it to work for the single
line input.


Adapted from:
http://placenamehere.com/Mozilla/js_textareas.html
http://www.xs4all.nl/~ppk/js/selected.html
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20110399.html

-- 
mailto:[EMAIL PROTECTED]
Thursday, August 21, 2003, 2:43:06 PM, you wrote:

PB> I know this can be done but I stink at java script. I want to select some
PB> text in a text field. Then click a graphic on the page and have it put an
PB> HTML tag around the selected text in the form field. Any idea on how to do
PB> this or where to find the code that does this?

PB> Phillip B.

PB> www.LoungeRoyale.com
PB> www.FillWorks.com

PB> 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/lists.cfm?link=t:4
Subscription: http://www.houseoffusion.com/lists.cfm?link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Get the mailserver that powers this list at 
http://www.coolfusion.com

Reply via email to