Math Jazz — Mathias Bynens’s shizzle, y’all



Note: This site might seem inactive… That’s because it is. Don’t worry though, I’m still coding webpages and stuff! If you’re interested, I suggest you get a translator and head over to Qiwi; or you could just check the latest site we’ve been working on: Apotheek Goethals – Debrabandere. Enjoy!

Textarea Resizer JavaScript

The comments textarea resizer is a very popular piece of JavaScript in the world of bloggers. I first saw it in use on Dunstan’s — I even think he wrote the script. (I’m not sure though, as there’s no blog post making it official or anything.)

The old-school document.write() method

Until recently, I was happily using this code:

document.write('<br /><small>(Comment textarea: <a style="cursor: pointer;" onclick="document.getElementById(\'comment\').rows += 5;" title="Click to enlarge the comment textarea">Increase size &#8595;</a> | <a style="cursor: pointer;" onclick="document.getElementById(\'comment\').rows -= 5;" title="Click to decrease the comment textarea">Decrease size &#8593;</a>)</small>');

I then simply placed a call to the script using some basic XHTML (<script type="text/javascript" src="/include/textarea-resizer.js"></script>), which was not semantic at all. But hey, it worked.

The document.createElement() method

Lately, I’ve been busy converting this site to application/xhtml+xml, which is in fact the correct MIME type for XHTML. One of the problems I ran into whilst doing that, is that when JavaScript is used with XHTML, document.write() doesn’t work. This means that in XML mode, we can’t use the above code anymore. However, document.createElementNS() still works.

In an attempt to make this script work with IE as well, let’s use the createElement() function.

function createElement(element) {
 if (typeof document.createElementNS != 'undefined') {
  return document.createElementNS('http://www.w3.org/1999/xhtml', element);
  }
 if (typeof document.createElement != 'undefined') {
  return document.createElement(element);
  }
 return false;
}

Now, let’s rewrite the code we were using before. Basically, all we need is two links; one to enlarge the textarea, and one to increase its size. Here’s what I came up with:

function textareaResizer() {
 var labels = document.getElementsByTagName('label');
 for(i=0; i<labels.length; i++) {
  if(labels[i].getAttribute("for") == "comment") {
   enlarge = createElement('a');
   enlarge.setAttribute('style', 'cursor: pointer;');
   enlarge.onclick = function () { document.getElementById('comment').rows += 5; }
   enlarge.setAttribute('title', 'Click to enlarge the message textarea');
   enlarge.appendChild(document.createTextNode('Enlarge textarea \u2193'));
   decrease = createElement('a');
   decrease.setAttribute('style', 'cursor: pointer;');
   decrease.onclick = function () { document.getElementById('comment').rows -= 5; }
   decrease.setAttribute('title', 'Click to decrease the message textarea');
   decrease.appendChild(document.createTextNode('Decrease textarea \u2191'));

   small = createElement('small');
   small.appendChild(document.createTextNode(' ('));
   small.appendChild(enlarge);
   small.appendChild(document.createTextNode(' | '));
   small.appendChild(decrease);
   small.appendChild(document.createTextNode(')'));
   labels[i].appendChild(small);
  }
 }
}

window.onload = textareaResizer;

Note that there’s no need to directly call the script from the XHTML file. Hurrah, semantics! You will need a label for a textarea named comment, though (I have <label for="comment">Your comment:</label> in my comment form). You can see an example below. If you want to try it out on other sites, you could use the User Script extension for Firefox, and put the above code in your userScript.js file. This should work on every non-Kubrick WordPress weblog.

I’m posting this mainly for my own reference, and of course to discuss the techniques used — I’m no JavaScript guru at all.

Filed under JavaScript, XHTML · February 8th, 2005

Comments (21)

Listed below are the responses for this entry.

  1. Rob Mientjes:
    This commenter’s Gravatar

    Mathias, it’s a good thing you think of the JS-enabledness. But I don’t care ;)

    Comment posted on February 8th, 2005 @ 9:20 pm
  2. Mathias:
    This commenter’s Gravatar

    Whaddya mean? Are you saying that you just assume your visitors have JS enabled? Are you? You might be right! I guess it’s a whole lotta easier to just not care. Like with IE, that is. Fuck the JS-less, CSS-less, Firefox-less bastards! In fact, it’s their problem. JavaScripts like this only add functionality to a web page…
    Nah, scratch that. You can’t deny that your current solution is pretty unsemantic… :P

    (Haven’t we been through this before?)

    The main (not to say: the only) reason I rewrote that old script, is to make it work with application/xhtml+xml. I didn’t really care about that script element being there…

    Comment posted on February 8th, 2005 @ 9:37 pm
  3. Turnip:
    This commenter’s Gravatar

    I think it’s a good idea, and I wish I could be assed to put it on my site ;). I will when I redesign.

    Actually, I have an awesome DHTML idea for my redesign :D. (Although it might not work, have to wait and see…)

    Comment posted on February 8th, 2005 @ 11:07 pm
  4. Rob Mientjes:
    This commenter’s Gravatar

    Mathias, I obviously am cocking around. I think I’ll use it with my redesign. Happy now? ;)

    Comment posted on February 9th, 2005 @ 12:34 am
  5. Dante Evans:
    This commenter’s Gravatar

    I might put on my site too, built-in to the default template of my CMS (which went into Alpha 2 today!).

    Remember, be nice to Opera users too. Firefoxless != Good-Browserless.

    Comment posted on February 9th, 2005 @ 2:18 am
  6. Colin D. Devroe:
    This commenter’s Gravatar

    Very nice write-up Mathias. I’m happy to see that the “overly anal” of us actually give back to the community by making their work public. Thank you.

    Comment posted on February 9th, 2005 @ 5:46 am
  7. Mathias:
    This commenter’s Gravatar

    Jon,

    Actually, I have an awesome DHTML idea for my redesign :D. (Although it might not work, have to wait and see…)

    I take it that’s all you’re going to tell us?

    Rob,

    Happy now? ;)

    Yup.

    Dante, that’s correct, Firefoxless ≠ Good-Browserless. But anyway, you got my point, right?

    Colin,

    I’m happy to see that the “overly anal” of us actually give back to the community by making their work public.

    Say what? Overly anal, moi?! ;) You know what? Work is public from the moment you start using it on a web site. So, I thought I could as well just make a post about it. :)

    Thanks for the comments. It’s always nice to get some feedback on something you’ve been working on.

    Comment posted on February 9th, 2005 @ 9:16 am
  8. Turnip:
    This commenter’s Gravatar

    Yeah, that’s all I’m going to say. I always seem to end up giving away my great ideas and it’s well… annoying.

    Comment posted on February 9th, 2005 @ 4:56 pm
  9. Indranil:
    This commenter’s Gravatar

    Great man. Must upgrade.

    By the way, you do too much to please Rob ;)

    Comment posted on February 11th, 2005 @ 6:15 am
  10. Aankhen:
    This commenter’s Gravatar

    enlarge.setAttribute('onclick', 'document.getElementById('comment').rows += 5;'); doesn’t look all that good.

    How about this instead: enlarge.onclick = function () { document.getElementById("comment").rows += 5; }?

    I generally shy away from executing any string as JavaScript code. I could be splitting hairs, but hey, that’s what I’m best at. ;)

    P.S.: A preview would be neat for those of us who are consumed with trying to look cool plagued with a need to keep editing their comments. :D

    Comment posted on February 13th, 2005 @ 3:14 pm
  11. Mathias:
    This commenter’s Gravatar

    Thanks for that, Aankhen. I kinda felt dirty when I wrote that line, but hey — it’s fixed now! Remember, there’s no splitting hairs when it comes to designing with web standards.

    Oh, and this site used to have a comment preview a couple of designs ago. I might look into writing something that’s application/xhtml+xml compatible… Any help is of course greatly appreciated :)

    Comment posted on February 15th, 2005 @ 8:24 pm
  12. Aankhen:
    This commenter’s Gravatar

    Oh, and this site used to have a comment preview a couple of designs ago. I might look into writing something that’s application/xhtml+xml compatible… Any help is of course greatly appreciated :)

    Hrm, not quite sure I follow. What would break under application/xhtml+xml? I’m not a big fan of live previews, a la Mike Davidson, to be honest; a simple “Preview” button which either takes me to a new preview page or (preferably) uses XmlHttpRequest to get the result would be just great.

    If you do mean a live preview, you might look into the DOMParser (sorry, don’t have a link as I’m not sure whether it’s in a specification) or importNode() from Mark Wubben.

    Comment posted on February 17th, 2005 @ 7:09 pm
  13. Mathias:
    This commenter’s Gravatar

    Hrm, not quite sure I follow. What would break under application/xhtml+xml?

    That would be document.write().

    I’m not a big fan of live previews, a la Mike Davidson,
    to be honest; a simple “Preview” button which either takes me to a new
    preview page or (preferably) uses XmlHttpRequest to get the result
    would be just great.

    *Drools* An XmlHttpRequest-wise comment preview would be so very cool! However, if I ever decide to go for a PHP preview thing, I shall never force my visitors to preview their comment first (by using both a “Preview” and a “Submit” button).

    Comment posted on February 19th, 2005 @ 9:02 am
  14. Børge:
    This commenter’s Gravatar

    Hi! I’m trying to get this thing to work on my blog, but I just can’t make it work. I’ve no experience either JavaScript or much other webdesign stuff, so I might have done much wrong. And that’s why I’m asking you for help! One place where I want it to work is here and you can find the .js file here. The text “kommentar:” has the label comment. Would you please help me figure out what I’m doing wrong? Thank you very much!

    Comment posted on February 25th, 2005 @ 3:37 am
  15. Mathias:
    This commenter’s Gravatar

    Hello Børge. As stated above, you have to include the createElement() function for the textarea resizer to work.

    Comment posted on February 25th, 2005 @ 8:45 pm
  16. Børge:
    This commenter’s Gravatar

    Thank you so much! I forgot about that. It works perfectly now.

    Comment posted on February 26th, 2005 @ 3:53 am
  17. Børge:
    This commenter’s Gravatar

    Hi again… Just one more question: I can see that on your site, when I click “Decrease textarea” the rest of the page follows. But on my site that only happens when I enlarge it. The text underneath the textarea (and then also the rest of the site) just stays down there when I decrease it again. Do you know/could you tell me how to fix that? Thanks!

    Comment posted on February 28th, 2005 @ 12:34 am
  18. Mathias:
    This commenter’s Gravatar

    There’s something seriously wrong with your CSS… Look into it, try avoiding position: absolute; as much as possible.

    Comment posted on February 28th, 2005 @ 1:39 pm
  19. Frodo Larik:
    This commenter’s Gravatar

    The resizer doesn’t seem to work in Safari… I wrote a more generic version, which should work in any browser. It’s available here.

    Sincerely,

    Frodo Larik

    Comment posted on March 14th, 2005 @ 4:13 pm
  20. Mathias:
    This commenter’s Gravatar

    Yeah, hobbit, my script indeed does not work in Safari, nor does it in IE… However, I must say that I’m not a big fan of <input type="button" onclick="replace_textarea('+');" value="+" />, aka executing a string as JavaScript code either.

    Comment posted on March 16th, 2005 @ 3:52 pm
  21. Viper007Bond:
    This commenter’s Gravatar

    Thanks for this! :D

    Comment posted on February 3rd, 2006 @ 5:18 pm

Trackbacks & Pingbacks (4)

Listed below are resources on the web that mention this article.

  1. Perlworld: Resize Textarea:
    This commenter’s Gravatar

    Resize Textarea
    In a scary bout of premonition, I was taking my daily look at the blog elite when I stumbled across the Textarea Resizer JavaScript over at mathibus.com.

    Trackback made on February 9th, 2005 @ 2:37 pm
  2. Turnip’s Patch: Now with added “2005”:
    This commenter’s Gravatar

    […] moved to WordPress plugins repository. Stopped the plugin working in feeds. The textarea resizer now uses the DOM (I’m predicting a gleeful comment from M […]

    Pingback made on March 3rd, 2005 @ 6:27 pm
  3. ennovy.org: Now you can write longer comments:
    This commenter’s Gravatar

    Now you can write longer comments
    I added in some code to my comments pages that will increase/decrease the size of the textarea in the comments form. […]

    Trackback made on April 22nd, 2005 @ 8:02 am
  4. Sunfox: Links for 2005-05-16:
    This commenter’s Gravatar

    Links for 2005-05-16
    […] Textarea Resizer JavaScript | Math Jazz (tags: javascript script programming) […]

    Trackback made on May 16th, 2005 @ 7:19 am