/    Sign up×
Community /Pin to ProfileBookmark

How to add multiple like and dislike buttons in a webpage?

I am having a page in which I am showing the like and dislike button in a particular div , it is working quite good for one like and dislike button but not for ,multiples.
Here is the code :
` <script>var btn1 = document.querySelector(‘.green’);
var btn2 = document.querySelector(‘.red’);
btn1.addEventListener(‘click’, function() {
if (btn2.classList.contains(‘red1’)) {
btn2.classList.remove(‘red1’);
}
this.classList.toggle(‘green1’);
});
btn2.addEventListener(‘click’, function() {
if (btn1.classList.contains(‘green1’)) {
btn1.classList.remove(‘green1’);
}
this.classList.toggle(‘red1’);
});
</script>
<style>
.btn{
cursor: pointer;
outline: 0;
color: #AAA;
padding:5px 10px;margin-bottom:0;background-color:rgb(240, 240, 240);
}
.btn i{font-size:16px;}
.btn:focus {background:transparent;}
.btn.green1{color: green;}
.green1{color: green;}
.btn.red1{ color: red;}
</style>
<div>
<button class=”btn green”><i class=”fa fa-thumbs-up fa-lg” aria-hidden=”true”></i></button>
<button class=”btn red”><i class=”fa fa-thumbs-down fa-lg” aria-hidden=”true”></i></button>
</div>
<div>
<button class=”btn green”><i class=”fa fa-thumbs-up fa-lg” aria-hidden=”true”></i></button>
<button class=”btn red”><i class=”fa fa-thumbs-down fa-lg” aria-hidden=”true”></i></button>
</div>`

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMar 07.2020 — This javascript should do the job:
const greenBtns = document.querySelectorAll('.btn.green'),
redBtns = document.querySelectorAll('.btn.red');
greenBtns.forEach((item, idx) =&gt; {
item.addEventListener('click', () =&gt; {
item.nextElementSibling.classList.remove('red1');
item.classList.toggle('green1');
});
});
redBtns.forEach((item, idx) =&gt; {
item.addEventListener('click', () =&gt; {
item.previousElementSibling.classList.remove('green1');
item.classList.toggle('red1');
});
});

Note that the sequence of the buttons has to be kept and that they have to be direct siblings. If this is not possible, the script would have to be modified a bit.
Copy linkTweet thisAlerts:
@nettiehunterMar 30.2020 — That's a pretty decent solution, actually. Thank you.

[happy wheels](https://happywheels2.io)
×

Success!

Help @shobha spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...