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 Comments

  1. gravatar Aryo Sanjaya - May 22, 2006

    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

  2. gravatar v n u z - May 22, 2006

    tetep jadi textpattern-ngelist ya mas…. :D

  3. gravatar ferdhie - May 22, 2006

    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

  4. gravatar isdah - May 24, 2006

    WP hokeh banget… nyoba tab

    weh koq gag isa…

  5. gravatar ferdhie - May 26, 2006

    Untuk comment emang belum saya implementasikan. Jek repot jess

  6. gravatar Aryo Sanjaya - May 26, 2006

    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

  7. gravatar jaylangkung - June 4, 2006

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

  8. gravatar jaylangkung - June 4, 2006

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

  9. gravatar Jauhari - June 26, 2006

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

  10. gravatar ferdhie - June 26, 2006

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

  11. gravatar shincan - July 22, 2006

    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

  12. gravatar anakbungsu - August 10, 2006

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

Comments are closed for this entry