Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted May 10, 2023, 12:55 PM EDT
Hi Try this for your ubderstanding ill split HTML,CSS and JS code seperately.
HTML CODE:l
CSS CODE:
#like-button, #unlike-button { padding: 10px; border: 1px solid #ccc; cursor: pointer; display: inline-block; } #like-button:hover, #unlike-button:hover { background-color: #f2f2f2; } #like-count { margin-top: 10px; }JS CODE:
let likes = 0; function like() { likes++; document.getElementById('like-count').innerHTML = likes; document.getElementById('like-button').style.display = 'none'; document.getElementById('unlike-button').style.display = 'inline-block'; } function unlike() { likes--; document.getElementById('like-count').innerHTML = likes; document.getElementById('like-button').style.display = 'inline-block'; document.getElementById('unlike-button').style.display = 'none'; }Hope this helps give it a try
Rajkiran SwainRank #165 · 14.4K reputationPosted May 10, 2023, 1:49 PM EDT
To add like and unlike buttons in a JS project, you can follow these steps:
1. Create two buttons in your HTML file, one for like and one for unlike. Give them unique IDs to identify them later. For example:
```html
```
2. Add an event listener to each button in your JavaScript file. When the like button is clicked, increment a counter variable and display the updated count. When the unlike button is clicked, decrement the counter variable and display the updated count. For example:
```javascript
let count = 0;
document.getElementById("like-btn").addEventListener("click", function() {
count++;
document.getElementById("count-display").innerHTML = count;
});
document.getElementById("unlike-btn").addEventListener("click", function() {
count--;
document.getElementById("count-display").innerHTML = count;
});
```
3. Add a display element in your HTML file to show the count of likes and unlikes. For example:
```html
Likes: 0
```
4. Style the buttons and count display element with CSS as desired.
Note: This is a basic example of how to implement like and unlike buttons in JS. Depending on the specifics of your project, you may need to modify the code or add additional functionality.
Deepak RawatRank #646 · 3.7K reputationPosted May 11, 2023, 11:24 AM EDT
here is example you can try
.fa { font-size: 50px; cursor: pointer; user-select: none; } .fa:hover { color: darkblue; } Click on the icon to toggle between thumbs-up and thumbs-down (like/dislike): function myFunction(x) { x.classList.toggle("fa-thumbs-down"); }Tuhin PaulRank #7 · 71.9K reputationPosted May 12, 2023, 11:52 PM EDT
Part 1:
To add like and unlike buttons in JavaScript:
Tuhin PaulRank #7 · 71.9K reputationPosted May 12, 2023, 11:53 PM EDT
Part 2:
See the code to add like and unlike buttons in JavaScript:
const likeButton = document.querySelector('#like-button'); const unlikeButton = document.querySelector('#unlike-button'); likeButton.addEventListener('click', () => { // Increment the like count const likeCount = document.querySelector('#like-count'); likeCount.textContent = parseInt(likeCount.textContent) + 1; }); unlikeButton.addEventListener('click', () => { // Decrement the like count const likeCount = document.querySelector('#like-count'); likeCount.textContent = parseInt(likeCount.textContent) - 1; });This code will create two buttons, one for like and one for unlike. When the like button is clicked, the like count will be incremented. When the unlike button is clicked, the like count will be decremented. The like count will be updated on the page.