This document provides detailed information about all the HTML helper functions available in the js-helpers library.
Generates a complete HTML element with all attributes.
createElement(name, content, options = {})Parameters:
name(string): The name of the element to be createdcontent(HTMLElement|HTMLElement[]|DocumentFragment|string): The content used to populate the elementoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A complete HTML element
Example:
const div = createElement('div', 'Hello World', { class: 'greeting' });A shortcut to generate an anchor element.
a(text, url, options = {})Parameters:
text(string): The anchor text contenturl(string): A path used to create the URLoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A complete anchor element
Examples:
// Returns an element object of an anchor <a href="http://example.com">Example</a>
const link1 = a('Example', 'http://example.com');
// Returns an element object of an anchor <a class="button">Click Me</a>
const link2 = a('Click Me', null, {'class': 'button'});A shortcut to generate an image element.
img(src, options = {})Parameters:
src(string): An image URI to be set as the image sourceoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A complete image element
Example:
const image = img('path/to/image.jpg', { alt: 'Example Image' });Generates a text node.
text(text)Parameters:
text(string): The text to be generated as a text node
Returns:
- (Text): Generated text node
Example:
const textNode = text('Hello World');Generates a form element.
form(action = '', method = 'post', options = {})Parameters:
action(string): The action URL that the form submits tomethod(string): The method the form uses in the submissionoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A complete form element
Example:
const myForm = form('/submit', 'post', { class: 'contact-form' });Generates a form label element.
label(content, target = null, options = {})Parameters:
content(HTMLElement|HTMLElement[]|DocumentFragment|string): The content used to populate the elementtarget(string): A string representing the ID of form control elementoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A complete label element
Example:
const nameLabel = label('Your Name:', 'name-input', { class: 'form-label' });Generates a generic button.
button(content, options = {})Parameters:
content(HTMLElement|HTMLElement[]|DocumentFragment|string): The button contentoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A complete button element
Example:
const btn = button('Click Me', { class: 'btn', onclick: 'handleClick()' });Generates a reset button.
resetButton(content, options = {})Parameters:
content(HTMLElement|HTMLElement[]|DocumentFragment|string): The button contentoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A complete reset button element
Example:
const resetBtn = resetButton('Reset Form', { class: 'btn-reset' });Generates a submit button.
submitButton(content, options = {})Parameters:
content(HTMLElement|HTMLElement[]|DocumentFragment|string): The button contentoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A complete submit button element
Example:
const submitBtn = submitButton('Submit Form', { class: 'btn-submit' });Generates an input element of a given type.
input(type, name, value, options = {})Parameters:
type(string): The type of input field to generatename(string): The value of the name attributevalue(string): The value of the input fieldoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A generated input field
Example:
const emailInput = input('email', 'user_email', '', { required: true, placeholder: 'Enter your email' });Generates a text input field.
textInput(name, value = null, options = {})Parameters:
name(string): The value of the name attributevalue(string|null): The value of the input fieldoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A generated input field
Example:
const nameInput = textInput('username', '', { placeholder: 'Enter username' });hiddenInput
Generates a hidden input field.
hiddenInput(name, value = null, options = {})Parameters:
name(string): The value of the name attributevalue(string|null): The value of the input fieldoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A generated input field
Example:
const csrfToken = hiddenInput('csrf_token', 'abc123xyz');Generates a password input field.
passwordInput(name, value = null, options = {})Parameters:
name(string): The value of the name attributevalue(string|null): The value of the input fieldoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A generated input field
Example:
const pwdInput = passwordInput('password', null, { required: true, placeholder: 'Enter password' });Generates a file input field.
fileInput(name, value = null, options = {})Parameters:
name(string): The value of the name attributevalue(string|null): The value of the input fieldoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A generated input field
Example:
const uploadInput = fileInput('profile_picture', null, { accept: 'image/*' });Generates a text box field.
textarea(name, value = '', options = {})Parameters:
name(string): The value of the name attributevalue(string): The value of the attributeoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A generated input field
Example:
const comments = textarea('comments', 'Default text', { rows: 5, cols: 40 });Generates a radio button input with an optional label.
radio(name, checked = false, options = {})Parameters:
name(string): The name attribute value of the radio buttonchecked(boolean): Whether the radio button is checkedoptions(Object): A name/value list of attributes to add to the elementvalue(string): The value attribute of the radio button. Defaults to '1'labelOptions(Object): A name/value list of attributes to add to the label element, if createdlabel(string): The text for the label. If provided, a label element is createdencode(boolean): Whether to HTML-encode the label text. Defaults to true
Returns:
- (HTMLElement): The generated radio button element, optionally wrapped in a label
Example:
const maleRadio = radio('gender', false, { value: 'male', label: 'Male' });Generates a checkbox input with an optional label.
checkbox(name, checked = false, options = {})Parameters:
name(string): The name attribute value of the checkboxchecked(boolean): Whether the checkbox is checkedoptions(Object): A name/value list of attributes to add to the elementvalue(string): The value attribute of the checkbox. Defaults to '1'labelOptions(Object): A name/value list of attributes to add to the label element, if createdlabel(string): The text for the label. If provided, a label element is createdencode(boolean): Whether to HTML-encode the label text. Defaults to true
Returns:
- (HTMLElement): The generated checkbox element, optionally wrapped in a label
Example:
const agreeCheckbox = checkbox('agree', true, { label: 'I agree to terms' });Generates a boolean input of the given type.
booleanInput(type, name, checked = false, options = {})Parameters:
type(string): The type of input to be created;radioorcheckboxname(string): The name attribute of the input elementchecked(boolean): The checked state of the input elementoptions(Object): A name/value list of attributes to add to the elementvalue(string): The value of the input element. Defaults to '1'uncheck(string): A value for the unchecked state of a checkbox value. This will generate a hidden input with the value of this propertylabel(string): A label displayed next to the checkbox that is not HTML-encoded. The label wraps around the checkboxlabelOptions(Object): An array of HTML attributes for the label tag
Returns:
- (DocumentFragment|HTMLElement): The generated input elements
Example:
const customCheckbox = booleanInput('checkbox', 'newsletter', true, {
value: 'subscribe',
label: 'Subscribe to newsletter',
labelOptions: { class: 'checkbox-label' }
});Generates an input button.
buttonInput(label = 'Button', options = {})Parameters:
label(string): The text of the input buttonoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A generated input field
Example:
const btn = buttonInput('Click Me', { class: 'btn', onclick: 'handleClick()' });Generates a submit input button.
submitInput(label = 'Submit', options = {})Parameters:
label(string): The text of the input buttonoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A generated input field
Example:
const submitBtn = submitInput('Send Message', { class: 'btn-submit' });Generates a reset input button.
resetInput(label = 'Reset', options = {})Parameters:
label(string): The text of the input buttonoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): A generated input field
Example:
const resetBtn = resetInput('Clear Form', { class: 'btn-reset' });Generates a select box.
select(name, selection = null, items = {}, options = {})Parameters:
name(string): The name of the select boxselection(string|string[]): A string or array of strings to set the selected value(s)items(Object): A key/value pair for the option value and textoptions(Object): A name/value list of attributes to add to the element
Returns:
- (DocumentFragment): The generated select box
Example:
const countrySelect = select('country', 'us', {
'us': 'United States',
'ca': 'Canada',
'mx': 'Mexico'
}, { required: true });Generates a select list box.
listBox(name, selection = null, items = {}, options = {})Parameters:
name(string): The name of the select boxselection(string|string[]): A string or array of strings to set the selected value(s)items(Object): A key/value pair for the option value and textoptions(Object): A name/value list of attributes to add to the element
Returns:
- (DocumentFragment): The generated select box
Example:
const fruitListBox = listBox('fruits', ['apple', 'orange'], {
'apple': 'Apple',
'orange': 'Orange',
'banana': 'Banana',
'grape': 'Grape'
}, { multiple: true, size: 4 });Renders options for a select element.
renderSelectOptions(selection, items, options = {})Parameters:
selection(string|string[]): The value or values to be marked as selecteditems(Object): The items to be used as options in the select element, as a key-value pair where the key is the option value and the value is the display textoptions(Object): Additional options for rendering the select options
Returns:
- (DocumentFragment): A document fragment containing the elements
Example:
const options = renderSelectOptions('medium', {
'small': 'Small',
'medium': 'Medium',
'large': 'Large'
});Generates a list of checkboxes.
checkboxList(name, selection = null, items = {}, options = {})Parameters:
name(string): The name attribute of each checkboxselection(string|string[]|null): The selected checkboxes. A string for a single value or an array for multiple checkboxesitems(Object): The data items to be used to generate the checkboxes. The object keys are the checkbox valuesoptions(Object): A name/value list for the checkbox list container tagseparator(string): A string to be used as a separator between the checkboxesunselect(string): A value to be used to unselect all checkboxesitemOptions(Object): A name/value list of attributes to add to each checkbox
Returns:
- (DocumentFragment): The list of checkboxes to return
Example:
const toppings = checkboxList('toppings[]', ['cheese', 'pepperoni'], {
'cheese': 'Cheese',
'pepperoni': 'Pepperoni',
'mushrooms': 'Mushrooms',
'olives': 'Olives'
}, { separator: ' | ', unselect: 'none' });Generates a list of radios.
radioList(name, selection = null, items = {}, options = {})Parameters:
name(string): The name attribute of each radio fieldselection(string|null): The selected radio. A string for a single value or an array for multiple radiositems(Object): The data items to be used to generate the radio. The object keys are the radio valuesoptions(Object): A name/value list for the radio list container tag
Returns:
- (DocumentFragment): The list of radio to return
Example:
const sizeOptions = radioList('size', 'medium', {
'small': 'Small',
'medium': 'Medium',
'large': 'Large'
}, { separator: '<br>', encode: true });Generates an unordered list.
ul(items, options = {})Parameters:
items(HTMLElement[]|string[]): An array of elements to append to the unordered list itemoptions(Object): A name/value list for the list container tag
Returns:
- (HTMLElement): The generated list
Example:
const fruitList = ul(['Apple', 'Banana', 'Orange'], { class: 'fruit-list' });Generates an ordered list.
ol(items, options = {})Parameters:
items(HTMLElement[]): An array of elements to append to the ordered list itemoptions(Object): A name/value list for the list container tag
Returns:
- (HTMLElement): The generated list
Example:
const steps = ol(['Mix ingredients', 'Pour into pan', 'Bake for 30 minutes'], { class: 'recipe-steps' });Generates a table element.
table(content, options = {})Parameters:
content(HTMLElement|HTMLElement[]|DocumentFragment|string): The content to be placed inside the tableoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): The generated table element
Example:
const tableEl = table(tbody, { class: 'data-table', id: 'user-table' });Generates a table row element.
tr(content, options = {})Parameters:
content(HTMLElement|HTMLElement[]|DocumentFragment|string): The content to be placed inside the rowoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): The generated table row element
Example:
const rowEl = tr([td('John'), td('Doe'), td('30')], { class: 'user-row' });Generates a table cell element.
td(content, options = {})Parameters:
content(HTMLElement|HTMLElement[]|DocumentFragment|string): The content to be placed inside the celloptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): The generated table cell element
Example:
const cellEl = td('Cell content', { class: 'data-cell', colspan: '2' });Generates a table header cell element.
th(content, options = {})Parameters:
content(HTMLElement|HTMLElement[]|DocumentFragment|string): The content to be placed inside the header celloptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): The generated table header cell element
Example:
const headerCellEl = th('Name', { class: 'header-cell', scope: 'col' });Generates a table header section element.
thead(content, options = {})Parameters:
content(HTMLElement|HTMLElement[]|DocumentFragment|string): The content to be placed inside the header sectionoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): The generated table header section element
Example:
const headerRow = tr([th('Name'), th('Email'), th('Age')]);
const theadEl = thead(headerRow, { class: 'table-header' });Generates a table body section element.
tbody(content, options = {})Parameters:
content(HTMLElement|HTMLElement[]|DocumentFragment|string): The content to be placed inside the body sectionoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): The generated table body section element
Example:
const row1 = tr([td('John'), td('john@example.com'), td('30')]);
const row2 = tr([td('Jane'), td('jane@example.com'), td('25')]);
const tbodyEl = tbody([row1, row2], { class: 'table-body' });Generates a table footer section element.
tfoot(content, options = {})Parameters:
content(HTMLElement|HTMLElement[]|DocumentFragment|string): The content to be placed inside the footer sectionoptions(Object): A name/value list of attributes to add to the element
Returns:
- (HTMLElement): The generated table footer section element
Example:
const footerRow = tr(td('Total: 2 users', { colspan: '3' }));
const tfootEl = tfoot(footerRow, { class: 'table-footer' });Appends content to an element. The content type can be an HTMLElement or an array of HTMLElement, a DocumentFragment, or a string.
renderContent(element, content)Parameters:
element(HTMLElement): The element to append the content tocontent(HTMLElement|HTMLElement[]|DocumentFragment|string): The content used to populate the element
Example:
const div = document.createElement('div');
renderContent(div, 'Hello World');Sets the HTML element attributes.
setAttributes(element, attributes = {})Parameters:
element(HTMLElement): The element to add attributes toattributes(Object): A name/value list of attributes to add to the element
Example:
const div = document.createElement('div');
setAttributes(div, {
class: 'container',
id: 'main',
style: { color: 'red', fontSize: '16px' }
});Add class names to an element.
addClass(element, classList)Parameters:
element(HTMLElement): The element to modifyclassList(string[]): An array of class names to add to the element
Example:
const div = document.createElement('div');
addClass(div, ['active', 'highlight']);Remove class names from an element.
removeClass(element, classList)Parameters:
element(HTMLElement): The element to modifyclassList(string[]): An array of class names to remove from the element
Example:
const div = document.createElement('div');
div.className = 'active highlight';
removeClass(div, ['active']);
// div now only has 'highlight' class