Skip to content

Instantly share code, notes, and snippets.

@maettig
Forked from 140bytes/LICENSE.txt
Created December 20, 2011 12:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maettig/1501501 to your computer and use it in GitHub Desktop.
Save maettig/1501501 to your computer and use it in GitHub Desktop.
insertTags in 140byt.es

We all know the little toolbar buttons to insert something into a textarea, like BBCode, Wiki markup or HTML tags (fun fact: GitHub doesn't have a toolbar). One of the resources I used once was this German site but there are many more. I was wondering, how much of this nifty, widespread code could be squished into 140 bytes.

Currently, this is 158 154 bytes and I'm looking for your help. One thing I could do is to remove a.focus(); and +b.length but doing so I will loose a major feature. I'm looking for other ways to make this smaller.

function(a, b, c, d, e, f, g) //textarea, left and right tag
{
a.focus( //not really required but very helpful
d = 'selection');
f = a[e = d + 'End']; //get cursor position
g = a.value.split(''); //short replacement for substr
g[a[d += 'Start'] - 1] += b; //append both tags
g[f - 1] += c;
a.value = g.join(''); //set new text
a[e] = a[d] = f + b.length //set cursor position
}
function(a,b,c,d,e,f,g){a.focus(d='selection');f=a[e=d+'End'];g=a.value.split('');g[a[d+='Start']-1]+=b;g[f-1]+=c;a.value=g.join('');a[e]=a[d]=f+b.length}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Thiemo Mättig <http://maettig.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "insertTags",
"description": "Surrounds selected text in a textarea with tags, e.g. BBCode or Wikitext.",
"keywords": [
"bbcode",
"forms",
"tags",
"textarea",
"toolbar"
]
}
<!DOCTYPE html>
<button onclick="insertTags(a, &quot;'''&quot;, &quot;'''&quot;)">'''bold'''</button>
<button onclick="insertTags(a, &quot;''&quot;, &quot;''&quot;)">''italic''</button>
<button onclick="insertTags(a, '[[', ']]')">[[link]]</button><br>
<textarea cols="80" rows="20">Select a word and click a button.</textarea>
<script type="text/javascript">
function(a, b, c, d, e, f, g) //textarea, left and right tag
{
a.focus( //not really required but very helpful
d = 'selection');
f = a[e = d + 'End']; //get cursor position
g = a.value.split(''); //short replacement for substr
g[a[d += 'Start'] - 1] += b; //append both tags
g[f - 1] += c;
a.value = g.join(''); //set new text
a[e] = a[d] = f + b.length //set cursor position
}
var a = document.getElementsByTagName('TEXTAREA')[0];
</script>
@maettig
Copy link
Author

maettig commented Jan 16, 2012

Doing the same for Internet Explorer is incredible easy, including setting the focus and not losing the cursor position (78 bytes):

function(a,b,c){a=document.selection.createRange(a.focus());a.text=b+a.text+c}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment