To use select-js, you can either include the script from a CDN or install it via npm.
<script src="https://cdn.jsdelivr.net/npm/@shanto-islam/select-js/dist/select-js.min.js"></script>
$ npm install @shanto-islam/select-js
Then, include it in your project:
<script src="node_modules/@shanto-islam/select-js/dist/select-js.min.js"></script>
Here are some examples of how to use the select-js library:
Select an element or elements from the DOM:
const element = select('div'); // Selects the first <div> element
const elements = select('.class-name'); // Selects all elements with class 'class-name'
Once you've selected elements, you can manipulate them using the following methods:
Adds a class to the selected element(s).
select('p').addClass('highlight'); // Adds 'highlight' class to all <p> elements
Removes a class from the selected element(s).
select('.highlight').removeClass('highlight'); // Removes 'highlight' class from all elements
Toggles a class on the selected element(s).
select('p').toggleClass('active'); // Toggles 'active' class on all <p> elements
Sets the text content of the selected element(s).
select('#title').setText('New Title'); // Sets the text of the element with id 'title'
Adds an event listener to the selected element(s).
select('button').on('click', () => alert('Button clicked!'));
Sets an attribute for the selected element(s).
select('img').setAttribute('alt', 'Image description');
Removes an attribute from the selected element(s).
select('img').removeAttribute('alt');
Gets the value of an attribute from the first selected element.
const altText = select('img').getAttribute('alt');
Adds inline styles to the selected element(s).
select('p').addStyle({ color: 'red', fontSize: '20px' });
Removes a specific inline style from the selected element(s).
select('p').removeStyle('color');
Appends HTML content to the selected element(s).
select('#list').append('<li>New Item</li>');
Prepends HTML content to the selected element(s).
select('#list').prepend('<li>First Item</li>');
Removes the selected element(s) from the DOM.
select('.temp').remove();
Toggles the visibility of the selected element(s).
select('.hidden-element').toggleVisibility();
Replaces the selected element(s) with new HTML content.
select('#old-element').replaceWith('<div id="new-element">New Content</div>');
Here’s an example of chaining multiple methods to perform several operations on selected elements:
// Select elements and chain methods for manipulation
select('.my-class')
.addClass('active') // Adds 'active' class to elements with 'my-class'
.setText('Hello, World!') // Sets the text content to 'Hello, World!' for selected elements
.on('click', () => alert('Clicked!')); // Adds a click event listener that shows an alert
// Chaining multiple manipulations on selected elements
select('.item')
.addClass('highlighted') // Adds 'highlighted' class to all elements with 'item' class
.setAttribute('data-active', 'true') // Sets a data attribute for selected elements
.append('New Element') // Appends a new span element
.addStyle({ color: 'blue', fontWeight: 'bold' }) // Adds multiple inline styles
.toggleVisibility() // Toggles the visibility of selected elements
.on('mouseover', () => console.log('Mouseover Event Triggered')); // Adds a mouseover event listener
// A more complex example with chaining
select('#menu')
.addClass('open') // Adds 'open' class to the menu
.setText('Menu Opened') // Updates the menu text
.addStyle({ backgroundColor: 'green' }) // Changes the background color
.on('click', () => select('#menu').removeClass('open').setText('Menu Closed')); // Toggles menu state on click
To select all matching elements within a context, use the `selectAll` function:
const allItems = selectAll('li'); // Selects all <li> elements
const itemsInDiv = selectAll('li', document.querySelector('#container')); // Selects all <li> elements inside #container
This function returns an array of elements, allowing you to use array methods like `.forEach()`.
The `select-js` library includes built-in error handling to provide warnings and errors for incorrect usage:
throw new Error('Selector must be a string.');
console.warn('No elements found for selector: ' + type);