A JavaScript class can not only extend ordinary JavaScript classes, it can extend the DOM class HTMLElement. This allows you to define new types of HTML elements that can be created either directly in HTML or by JavaScript code.
This is illustrated in the two HTML inserts below. In the top one the elements are created directly in HTML as <counted-text> and <count-view> tags. In the bottom one they are created with JavaScript code using the create() methods defined in the CountedText and CountView classes.
In each, the text "Edit me!" is an element of the CountedText class, an extension of HTMLElement. You can edit it freely using the usual select, type characters, backspace, cut, copy, and paste gestures of a GUI text area.
The text "Count = 8." is an element of the CountView class, another extension of HTMLElement. JavaScript code makes it a listener for the CountedText instance, and it displays the character count for that instance.
These examples are described in the menu links to the left.
We want to define a new tag with tag name "counted-text". It could appear in an HTML as follows:
<counted-text id="my-counted-text" text="Edit me!"></counted-text>
The "id" attribute is a standard HTMLElement attribute. The "text" attribute specifies the initial text for the counted-text tag. It will need to be handled in
We also want to be able to construct a <counted-text> tag in JavaScript code. We will use the class name "CountedText" for such objects, defining them in the file CountedText.js. This class will extend the HTMLElement class.