2

I'm writing a notebook in HTML for my own personal use. When writing an HTML document, you can't simply indent paragraphs with tabs or spaces. Rather than having to copy-and-paste several &nbsp; entities every time I want to indent a paragraph, I thought I'd be clever and write a JavaScript function to print 4 spaces. Unfortunately, I'll still end up copying and pasting <script>tab_function();</script> every time I want to indent something, which in that case I might as well just paste the entities instead.

I'm wondering if there is a way to create a "macro" or alias (like #define in C++) to make this less of a burden. It would be nice if I could #define "TAB" to call this function, but alas I don't know if such a thing exists in HTML. Is this even possible?

Shikiryu
  • 10,180
  • 8
  • 49
  • 75
j_burks
  • 93
  • 5
  • So you are writing an html editor? Or a text editor written in html? This is not clear. Maybe you are just looking for the `
    ` tag
    – Steve Nov 01 '15 at 22:48
  • Nay, not an editor. Think of it as a book or manual written in HTML for easy navigation. There are images, tables, inline-blocks, etc. Many, many paragraphs.
     might be an interesting solution (feel dumb for not knowing about it until now, but at least I learned something), though I am still curious if something like  could be "macroed" into something short and easy to insert right in the middle of an HTML's body, rather than typing all of that in every time..
    – j_burks Nov 02 '15 at 00:26
  • While this might be a useful learnng exercise, you don't need JavaScript for this; there's a CSS [text-indent property](https://developer.mozilla.org/en-US/docs/Web/CSS/text-indent) that controls first-line indentation See https://jsfiddle.net/mbwbo3jz/ for an example. – Joshua Taylor Nov 04 '15 at 23:15

1 Answers1

3

You can simply write a event listener into your html.

document.onkeydown = function(e) {
    if(e.keyCode === 9){//9 = tab
         //add 4 spaces, textarea.value += "    "; perhaps?
         e.preventDefault();
    }
}

About macros, you can even sorta write your own compiler in javascript(compile to javascript I mean by that), by simply parsing text, basically anywhere.

Olavi Sau
  • 1,647
  • 13
  • 20