moni.js
is a minimal library for DOM manipulation and event handling. Below is a list of available methods with usage examples.
Include moni.js
in your project and start manipulating DOM elements using the moni
keyword.
<script src="path/to/moni.umd.js"></script>
Or, using npm:
npm i moni22
Or, using cdn:
<script src="https://cdn.jsdelivr.net/npm/moni22"></script>
Selects DOM elements.
moni('#myDiv'); // Select an element with ID 'myDiv'
moni('.myClass'); // Select elements with class 'myClass'
Gets or sets the HTML content of the selected element.
// Get HTML content
const content = moni('#myDiv').html();
// Set HTML content
moni('#myDiv').html('<p>Hello, World!</p>');
Attaches an event listener to the selected elements.
moni('#myButton').on('click', function() {
alert('Button clicked!');
});
Executes a callback for each selected element.
moni('.myClass').each(function(el) {
console.log(el);
});
Remove elements from the dom
moni('div').remove()
Get or set attributes
moni('button').on('click', function() {
const id = moni("#myDiv").css('color', 'red').attr('id');
moni('#myDiv').attr('data-abc', 'xyz');
});
Get or set dataset attributes
moni('button').on('click', function() {
const name = moni('div').data('name');
console.log(name);
});
Add new elements inside another element
moni('button').on('click', function() {
moni('div').add('<strong>Small</strong>', 4);
moni('div').add('<p>Paragraph</p>');
});
Get or set value for a given element
moni('form').on('submit', function(e) {
e.preventDefault();
const username = moni('input').val();
const coding = moni('select').val()
console.log(username);
console.log(coding);
moni('select').val('js');
moni('input').val('');
moni('textarea').val('message');
});
Get the first matched element
moni('p').first().css('color', 'red');
Get the last matched element
moni('p').last().css('color', 'red');
Get the element at the given index
moni('p').at(2).css('color', 'purple');
Grab a form's data simply
moni('form').on('submit', function(e) {
e.preventDefault();
const values = moni('form').values();
console.log(values);
});
Add an element before the matched element
moni('div').before('<div>Div 0</div>')
Add an element after the matched element
moni('div').after('<div>Div 1</div>')
Return a list of all the children of the matched element
const children = moni('div').children();
const firstChild = children.at(0); // .first();
Empty the contents of the matched element
moni('ul').empty();
Clone a matched element. By default deep cloning is set.
const cloned = moni('ul').clone(); // or clone(false);
moni('body').add(cloned);
Add an element to the previous of moni(selector)
moni('p').addPrevious('<p>Another paragraph</p>');
Add an element to the behind of moni(selector)
moni('p').addBehind('<div>Another paragraph</div>');
Select all the siblings of matched element
moni('ul li').siblings().css('color', 'red');
Search elements inside moni(selector)
moni('ul').search(
moni('.bad')
).css('color', 'red');
Find the nearest matching element.
moni('li').near(
moni('div')
).css('background-color', 'blue');
moni('li').near('div').css('background-color', 'blue');
Provides methods to interact with the class list of an element.
const hasClass = moni('#myDiv').classes().has('active');
Adds a class to the element.
moni('#myDiv').classes().add('active');
Removes a class from the element.
moni('#myDiv').classes().remove('active');
Toggle a class for the element.
moni('button').on('click', function() {
moni('#myDiv').classes().toggle('active');
});
Converts the class list to an array.
const classArray = moni('#myDiv').classes().toArray();
Gets or sets the CSS style of the selected elements.
// Get the 'color' property
const color = moni('#myDiv').css('color');
// Set the 'color' property
moni('#myDiv').css('color', 'red');
Find elements inside another thing.
moni('#div').find('p').after('<div class="ok">OK</div>')
Set or get text of an element
moni('div').on('click', 'div.ok', function () {
moni(this).text('text goes here')
const text = moni(this).text();
console.log({
text
})
});
Follow the example:
moni('form').on('submit', e => {
e.preventDefault();
const formData = moni('form').values();
moni()
.ajax()
.request('http://localhost:3000/users')
.type('POST')
.loading(() => console.log('Loading...'))
.header({
'Content-Type': 'application/json',
})
.send(formData)
.failed((error) => console.error(error))
.success((response) => console.log('Success:', response))
.end(() => console.log('Request finished'))
.execute();
});
Bootstrap is required for following things to work.
moni('#myModal').modal()
.show()
moni('#myModal').modal()
.hide()
moni('#myModal').modal()
.toggle()