views:

392

answers:

6

I want to have a textarea which displays line numbers on the left. Wrap should be set to "off" (so that horizontal scrolling is available). I want to have my page as a single self-contained .html file (there will be no graphics), so I'd like to avoid any 3rd party frameworks.

Which way should I go? How would you do this?

+2  A: 

I think your best bet would be to just have a regular textarea, and show a preview (like stack overflow) as they type along. Within that preview, you could easily add line numbers on each line and format the look via CSS.

Zack
Sorry, no, I want the user to have them while typing. The textarea will contain special information, one item per line, and the line number will be used for identifying the item.
Vilx-
A: 

You'd have to wrap the text area and capture the keystrokes looking for newline characters (Eg ENTER). Doing a quick Google search and you get http://www.dhtmlgoodies.com/forum/viewtopic.php?t=506

Josh Kerr
A: 

Personally I'd just have a regular textarea and a div down the left side that displays the numbers (possibly auto-adjusting as more carriage returns get added to it).

Noon Silk
+2  A: 

Another simple method is to create a narrow, but tall background image and some left padding on the textarea. This has the minor caveat of having a fixed number of rows and font style, but it is the option with the least amount of code required.

Jim H.
+1  A: 

Without images, that's a tough one.

Best bet: Place a second textarea with identical settings (font size, line height, padding...) but different styling (no background color, no borders) to the left of your original textarea. Make it read only, take it out of the tab rotation (tabindex=99999 might do the trick or simply disabling it), and put line numbers in it. This should work well and correctly as far as I can think, it should even survive things like the client resizing the font manually in their browser.

You could even use position: relative and a big padding-left: value in the original textarea to move the counter textarea into the original one.

Downside: The line counter won't follow vertical scrolling of the textarea. See the comments below.

Pekka
Wouldn't just disabling it take it out of tab rotation?
pimlottc
True. Changed my answer accordingly, cheers.
Pekka
You think that a DIV with the line numbers wouldn't work?
Vilx-
There is one problem I see with such a solution - if a user started to select the line numbers with the mouse, he could (at least in some browsers like Opera) scroll away the line container without scrolling the main container.
Vilx-
That could probably be dealt with by making the container `unselectable`, but you point out a much bigger flaw: The left hand textarea won't follow vertical scrolling. So, one would have to make the textareas both so tall that they won't scroll, and handle the scrolling using a surrounding `div` with `overflow: auto`.
Pekka
@Vilx: re DIV: It's safer to use a textarea because you catch all textarea relevant CSS settings made elsewhere (e.g. `textarea { line-height: 1.9em }`)
Pekka
+1  A: 

I would start with two text-areas and synchronzation mechanism. Something like this,

    <script>
    window.sync = function(e){
         var textarea = document.getElementById("lines");
         var source   = document.getElementById("some_text_area");
         textarea.scrollTop = source.scrollTop;
        }

        window.populate = function populate(){
         if(populate.started){
          return;
         }

         populate.started = true;
         var textarea = document.getElementById("lines");
         var str = '';
         for(var i=0;i < 100;i++){
          str = str + (i +'\r\n');
         }
         textarea.value = str;
        }
    </script>

<div>
<textarea 
    style="width:40px;overflow:hidden;height:40px;" 
    readonly="true" 
    id="lines"
></textarea>
<textarea 
    style="width:500px;height:40px;" 
    id="some_text_area" 
    onclick="populate()"
    onscroll="sync();"
></textarea>
</div>

Ofcourse populate() function (and the style declaration and event declaration) should be more robust and intelligent, , but, it just for showcase purpose.

nemisj
would a DIV instead of a second TEXTAREA work too?
Vilx-
Yes, but I think, in some situations, when line-height is different, it would be not really neatly lined out.
nemisj