Thumbs Up with JavaScript: Enhance User Engagement

In the vast world of web development, JavaScript has emerged as a powerful tool, with its capabilities extending far beyond basic functionality. One such feature is the use of the "thumbs up" or liking system, which has become a standard interaction method on countless websites and apps. In this guide, we will explore how to implement a simple "thumbs up" system using JavaScript, ensuring the code is clean, efficient, and SEO-friendly.

“Thumbs Up”
“Thumbs Up”

Before delving into the code, it's crucial to understand the basic concept. A "thumbs up" system usually involves a button or icon that users can click to express their approval or agreement. This button should increment a counter display whenever it's clicked, reflecting the number of users who have liked the content.

a thumbs up icon on a blue background
a thumbs up icon on a blue background

Setting Up the HTML and CSS

First, let's set up the HTML and CSS structure. We'll use a simple button and a div to display the likes count.

http://www.rescuebit.com/ Bookmarklet to use for pinning: javascript:void((function(){var%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','http://assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})()); Yellow Emoji Humor, Happy Face Quote, Ad Emoji Meme, Cool Guy Emoticon, Emotiguy Meme, Emotiguy Sunglasses, Goofy Happy Face Emoji, Smiley Emoticon, Confident Emoji Meme
http://www.rescuebit.com/ Bookmarklet to use for pinning: javascript:void((function(){var%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','http://assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})()); Yellow Emoji Humor, Happy Face Quote, Ad Emoji Meme, Cool Guy Emoticon, Emotiguy Meme, Emotiguy Sunglasses, Goofy Happy Face Emoji, Smiley Emoticon, Confident Emoji Meme

```html

0
```

Using inline styles for simplicity

the thumbs up sign is yellow and black with two eyes, one has an emoticive look on it's face
the thumbs up sign is yellow and black with two eyes, one has an emoticive look on it's face

For this example, we'll use inline styles to make the code SEO-friendly and clean. In a real-world application, you'd want to use external stylesheets for better maintainability.

```css #likes-count { font-size: 20px; margin-right: 10px; } #thumbs-up { background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; } ```

Implementing the JavaScript Functionality

a thumbs up sign with the thumb down
a thumbs up sign with the thumb down

Now that we have our basic structure, let's add the JavaScript functionality. We'll use the `addEventListener` method to detect button clicks and update the likes count accordingly.

```javascript const likesCountDisplay = document.getElementById('likes-count'); const thumbsUpButton = document.getElementById('thumbs-up'); let likesCount = 0; thumbsUpButton.addEventListener('click', function() { likesCount++; likesCountDisplay.innerText = likesCount; }); ```

Preventing multiple clicks without refreshing the page

Thumbs up
Thumbs up

To prevent users from clicking the "thumbs up" button multiple times without refreshing the page, we can disable the button after the first click. Here's how you can do it using JavaScript:

```javascript thumbsUpButton.addEventListener('click', function() { likesCount++; likesCountDisplay.innerText = likesCount; thumbsUpButton.disabled = true; // Disable the button after the first click }); ```

Best Practices When Working With JavaScript Templates | Envato Tuts+
Best Practices When Working With JavaScript Templates | Envato Tuts+
a smiley face wearing sunglasses giving the thumbs up
a smiley face wearing sunglasses giving the thumbs up
thumbs up
thumbs up
a man giving the thumbs up sign at an event
a man giving the thumbs up sign at an event
an image of a hand cursive with the thumb up icon on it's side
an image of a hand cursive with the thumb up icon on it's side
a thumbs up with a smiley face drawn on it's thumb and two fingers
a thumbs up with a smiley face drawn on it's thumb and two fingers
Hand, Thumbs Up Vector Logo Like by Bureau | Creative Market
Hand, Thumbs Up Vector Logo Like by Bureau | Creative Market
an image of a thumbs up sign made out of 3d paper on a computer screen
an image of a thumbs up sign made out of 3d paper on a computer screen
a thumbs up sign with the thumb down
a thumbs up sign with the thumb down

Resetting the likes count on page refresh

In a real-world application, you'd want to store the likes count in a database, allowing it to persist even after page refreshes. However, for this simple example, we'll just reset the count to 0 when the page is refreshed. Here's how you can do it using the `onbeforeunload` event:

```javascript window.onbeforeunload = function() { likesCount = 0; }; ```

Congratulations, you've now created a simple "thumbs up" system using JavaScript! This system is not only functional but also SEO-friendly, as it uses clean, semantic HTML and does not rely on JavaScript for basic functionality. Always remember to test your code extensively and make sure it works well on different browsers and devices.