All Tutorials

Your One-Stop Destination for Learning and Growth

Creating and Sharing a Button with Count using Donreach

In today's digital world, creating interactive web components is an essential skill for any developer. In this tutorial, we will walk you through the process of building a custom share button with a count using Donreach, a lightweight JavaScript library.

Prerequisites

Before we dive into the code, make sure you have the following tools installed:

  1. Text Editor or IDE: You'll need a text editor like Sublime Text, Visual Studio Code, or an Integrated Development Environment (IDE) such as WebStorm or Visual Studio.
  2. Basic HTML and JavaScript knowledge: This tutorial assumes you have a working understanding of HTML and JavaScript. If not, we recommend checking out some introductory resources first.

Setting up the HTML structure

First, create an index.html file with the following basic structure:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Share Button Example</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <!-- Your HTML content goes here -->
    <script src="donreach.min.js"></script>
    <script src="app.js"></script>
  </body>
</html>

Creating the Share Button

Let's create a simple share button with an initial count of 1. Add the following code snippet inside the body tag:

<button id="share-btn">Share (1)</button>
<div id="count"></div>

Setting up Donreach

Donreach is a simple library for creating custom event listeners. First, create an app.js file and include the following code to initialize Donreach:

const donreach = require('donreach');

// Initialize Donreach with a custom prefix (optional)
donreach.init({ prefix: 'myApp' });

Creating the Share Button Functionality

Now, let's create the functionality for our share button. Add the following code to app.js:

let count = 1;
const shareBtn = document.getElementById('share-btn');
const countDisplay = document.getElementById('count');

// Create a click event listener using Donreach
donreach.on(shareBtn, 'click', handleShareClick);

function handleShareClick() {
  count++;
  updateCountDisplay();
}

function updateCountDisplay() {
  countDisplay.textContent = `Share (${count})`;
}

This code sets up a click event listener for the share button and increments the count whenever it's clicked. It also updates the display to show the new count.

Styling the Button (Optional)

You can customize the appearance of your share button by adding CSS in the styles.css file or directly inside the <style> tags in the HTML file. For example:

button {
  background-color: #4CAF50;
  color: white;
  padding: 12px 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}

Conclusion

With this tutorial, you've learned how to create a custom share button with a count using Donreach and JavaScript. This simple yet powerful technique can be easily extended for more complex use cases such as sharing on social media platforms or implementing real-time counters. We hope this tutorial has inspired you to explore the possibilities of interactive web components!

Happy coding!

Published August, 2016