Introduction to Dramjs: A Versatile JavaScript Library
Dramjs is a powerful and lightweight JavaScript library designed to simplify and accelerate web development tasks. Its rich set of APIs can help developers handle DOM manipulations, event handling, and AJAX requests with ease. In this article, we will explore some of the most useful APIs provided by Dramjs along with practical code examples.
Key APIs of Dramjs with Examples
1. DOM Manipulation
Dramjs makes it straightforward to manipulate the DOM with its intuitive methods.
// Adding a new element
dram('body').append('<div>New Element</div>');
// Changing the text of an element
dram('#elementId').text('New Text');
// Changing the HTML content of an element
dram('#elementId').html('<span>New HTML Content</span>');
2. Event Handling
Handling events in Dramjs is simple and effective.
// Adding a click event listener
dram('#buttonId').on('click', function() {
console.log('Button clicked!');
});
// Removing an event listener
dram('#buttonId').off('click');
3. AJAX Requests
Dramjs provides convenient methods to handle AJAX requests seamlessly.
// Making a GET request
dram.ajax({
url: 'https://api.example.com/data',
method: 'GET',
success: function(response) {
console.log(response);
},
error: function(error) {
console.error(error);
}
});
// Making a POST request
dram.ajax({
url: 'https://api.example.com/data',
method: 'POST',
data: {
key1: 'value1',
key2: 'value2'
},
success: function(response) {
console.log(response);
},
error: function(error) {
console.error(error);
}
});
4. Chaining Methods
One of the most powerful features of Dramjs is its ability to chain methods for greater readability and efficiency.
// Chaining methods for multiple actions
dram('body')
.append('<div>Chained Element</div>')
.find('div')
.css({ color: 'red' })
.text('Updated Text');
App Example: To-Do List
Here’s an example of a simple To-Do List app using the above APIs.
<!DOCTYPE html>
<html>
<head>
<title>To-Do List with Dramjs</title>
<script src="https://path.to/dramjs.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Add new to-do item
dram('#addBtn').on('click', function() {
const newItem = dram('#newItem').val();
if (newItem) {
dram('#toDoList').append(`<li>${newItem}</li>`);
dram('#newItem').val('');
}
});
// Remove a to-do item
dram('#toDoList').on('click', 'li', function() {
dram(this).remove();
});
});
</script>
</head>
<body>
<h1>To-Do List</h1>
<input type="text" id="newItem" placeholder="New item">
<button id="addBtn">Add</button>
<ul id="toDoList"></ul>
</body>
</html>
Hash: 290887be668252d8c8ebbaf29b54d9d9448c2382155e3b584321c217d60fccc2