I want to create a system where a user can highlight some text, click a button and then the selected text is surrounded by some tags. sitepoint does it.

Could anyone point me in the right direction. Ive googled this but havent had any joy. Im pretty sure it'd be javascript