← Back to Index

$.t()

Static element creator helper.

Examples

1. Create element

const $btn = $.t('<button>', {
     class: 'btn btn-primary',
     text: 'Click Me',
     click: () => console.log('Hi')
});
$('body').append($btn);

Result

<button class="btn btn-primary">Click Me</button>

2. Create structure

const $card = $.t('<div>', {
     class: 'card',
     html: '<h1>Title</h1>',
     css: { border: '1px solid black' },
     data: { id: 101 }
});

Result

<div class="card" style="border: 1px solid black;">
     <h1>Title</h1>
</div>

Parameters

Parameter Type Description
html string An HTML string representing the element to create (e.g., <div>).
props object An object of properties, attributes, and events to apply. Supported keys:
  • class (string): Sets the class attribute.
  • text (string): Sets text content.
  • html (string): Sets inner HTML.
  • css (object): Sets CSS styles via .css().
  • data (object): Sets data properties via .data().
  • [event] (function): Binds an event handler (e.g., click: fn).
  • Any other key is set as an attribute or property (e.g., id, href, value).

Returns: A new SR object containing the created element.