Answer the question
In order to leave comments, you need to log in
How to change its class and content when clicking on an element, and return it back when clicked again?
Good afternoon!
<span class="badge bg-danger" style="cursor: pointer;">150</span>
<span class="badge bg-danger" style="cursor: pointer;">250</span>
<span class="badge bg-danger" style="cursor: pointer;">350</span>
<span class="badge bg-danger" style="cursor: pointer;">450</span>
Answer the question
In order to leave comments, you need to log in
As an option))
<span class="badge bg-danger" onclick="changeBadge(150, 200)" style="cursor: pointer;">150</span>
<span class="badge bg-danger" onclick="changeBadge(250, 255)" style="cursor: pointer;">250</span>
<span class="badge bg-danger" onclick="changeBadge(350, 235)" style="cursor: pointer;">350</span>
<span class="badge bg-danger" onclick="changeBadge(450, 225)" style="cursor: pointer;">450</span>
<script>
const changeBadge = (oldContent, newContent) => {
const badge = event.target
if (badge.classList.contains('bg-danger')) {
badge.classList.add('bg-success')
badge.classList.remove('bg-danger')
badge.innerHTML = newContent
} else {
badge.classList.remove('bg-success')
badge.classList.add('bg-danger')
badge.innerHTML = oldContent
}
}
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question