Skip to content

Chain

Chainable DOM manipulation class supporting element creation, attribute setting, event listening, etc.

API

Chain

Constructor

typescript
new Chain(tagName: string, options?: ElementCreationOptions)

Main Methods

MethodDescriptionReturn Value
setAttributeSet element attributeChain
removeAttributeRemove element attributeChain
appendAdd child elementChain
removeRemove child elementChain
setTextContentSet text contentChain
setStyleSet styleChain
addChildAdd child element (supports array)Chain
listenAdd event listenerChain
clearListenerRemove event listenerChain
clearAllListenerRemove all event listenersChain

Properties

PropertyDescriptionType
elementDOM elementHTMLElement

Example

Basic Usage

js
import { Chain } from 'ranuts';

const div = new Chain('div')
  .setAttribute('id', 'myDiv')
  .setAttribute('class', 'container')
  .setTextContent('Hello World')
  .setStyle('color', 'red');

document.body.appendChild(div.element);

Chaining

js
import { Chain } from 'ranuts';

const button = new Chain('button')
  .setAttribute('type', 'button')
  .setTextContent('Click Me')
  .setStyle('padding', '10px')
  .setStyle('background', 'blue')
  .listen('click', () => {
    console.log('Button clicked');
  });

document.body.appendChild(button.element);

Add Child Elements

js
import { Chain } from 'ranuts';

const container = new Chain('div')
  .addChild(new Chain('h1').setTextContent('Title'))
  .addChild(new Chain('p').setTextContent('Content'));

document.body.appendChild(container.element);

Batch Add Child Elements

js
import { Chain } from 'ranuts';

const list = new Chain('ul').addChild([
  new Chain('li').setTextContent('Item 1'),
  new Chain('li').setTextContent('Item 2'),
  new Chain('li').setTextContent('Item 3'),
]);

document.body.appendChild(list.element);

SVG Elements

js
import { Chain } from 'ranuts';

const svg = new Chain('svg').setAttribute('width', '100').setAttribute('height', '100');

const circle = new Chain('circle').setAttribute('cx', '50').setAttribute('cy', '50').setAttribute('r', '40');

svg.addChild(circle);

Notes

  1. Chaining: All methods return Chain instance, supporting chaining.
  2. SVG support: Automatically recognizes SVG tags, uses correct namespace for creation.
  3. Event management: Internally maintains event listener mapping for easy management and removal.
  4. Use case: Commonly used for dynamically creating DOM structures, building UI components, etc.

Released under the MIT License.