TAB di Textarea

Pernah coba masukin karakter TAB di Textarea? Bisa ngga? Kalo bisa aku kopi browsernya. Kenapa ngga bisa? Masalahnya karakter TAB diterjemahkan oleh browser untuk pindah kontrol, ini bener2 menjengkelkan bagi saya, saat ngedit tampilan di Textpattern. Akhirnya, saya buat JavaScript sederhana agar bisa masukin karakter TAB di textarea. Ini snippetnya, kopi paste aja

The Code

function textarea_tab(id)
{
    var textarea = document.getElementById(id);
    if (!textarea) return;
    textarea.onkeydown = function(e) {
        e = e ? e : window.event;
        if (e.keyCode == 9 && !e.shiftKey && !e.controlKey && !e.altKey) {
            if (document.selection) {
                this.focus();
                var sel = document.selection.createRange();
                sel.text = "\t";
                this.focus();
            }
            else if (this.selectionStart || this.selectionStart == "0") {
                // MOZILLA/NETSCAPE support
                var top = this.scrollTop;
                var startPos = this.selectionStart;
                var endPos = this.selectionEnd;
                var text = this.value;
                this.value = text.substring(0, startPos) + "\t" +
                    text.substring(endPos, text.length);
                this.selectionStart = startPos+1;
                this.selectionEnd = endPos+1;
                this.scrollTop = top;
            } else {
                // giveup
                this.value += "\t";
            }
            return false;
        }
    }
} 

Contoh pemakaian (Examples):

<textarea id="mytextarea" cols="80" rows="25"></textarea>
<script type="text/javascript"><!--
	textarea_tab('mytextarea');
//--></script>

12 komentar

  1. Aryo Sanjaya bilang:

    Kalo mau ngopi, nih browserku bisa… tapi mesti dikombinasi dengan Notepad!
    Jadi ngetiknya di Notepad, ntar kalo dah jadi di-copy-paste ke textarea, gettoo…

    mekso

    May 22nd, 2006 at 09:44
  2. v n u z bilang:

    tetep jadi textpattern-ngelist ya mas…. :D

    May 22nd, 2006 at 14:32
  3. ferdhie bilang:

    textpattern, wordpress, mt, typo, dsb cuman tool aja, saya lebih prefer ke textpattern cuman karena masalah section, category nya saja, tapi sepertinya, sekarang wordpress sudah ada static-pages ya

    May 22nd, 2006 at 17:24
  4. isdah bilang:

    WP hokeh banget… nyoba tab

    weh koq gag isa…

    May 24th, 2006 at 22:08
  5. ferdhie bilang:

    Untuk comment emang belum saya implementasikan. Jek repot jess

    May 26th, 2006 at 08:35
  6. Aryo Sanjaya bilang:

    URGENT: Buang the f**kin ‘preview’. Langsung submit napa?

    Bayangkan setiap kamu ngetik command di Shell BSD kudu preview dulu, baru boleh diexecute.

    lagi PMS

    May 26th, 2006 at 18:48
  7. jaylangkung bilang:

    kan memang harus di preview dulu toh om biar tau hasilnya :(

    June 4th, 2006 at 11:29
  8. jaylangkung bilang:

    btw function tak coba kok gak bisa?? efek e opo seh??? binun aku

    June 4th, 2006 at 11:33
  9. Jauhari bilang:

    Mas mas…. ini make Mesin Blog sendiri ya? keren mas. ;)
    atau make mesin BLOG?

    June 26th, 2006 at 08:57
  10. ferdhie bilang:

    hah? oh aku pake textpattern doang. mo bikin sendiri? ga sempet.. hehehe – bagi yg wordpress-elist – ga boleh marah!!!

    June 26th, 2006 at 09:10
  11. shincan bilang:

    hmm asek juga neh …
    eh mas kalo hunting tutorial di internet khususnya masalah PHP di mana yah .. kasih referensinya donk …

    kirimin aja ke email ku ya mas .. ga mungkin aku buka² replay dari postingan comment ini.. soalnya abis keluar dari warnet aku mesti wes lali opo iku mau webiki .. hahakz :D

    thx

    July 22nd, 2006 at 12:49
  12. anakbungsu bilang:

    mas kok di firefox enggak jalan seh ???
    klo di IE sih it’s ok fine2 sajahhhh….
    hehehehee

    August 10th, 2006 at 12:55