I want to toggle a class to a svg inside a button tag on click of this exact button.
Here is the html code for my button:
<button class="control-btn" id="control-btn-edit">
<svg id="edit-svg" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 528.899 528.899" xml:space="preserve"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <g> <path d="M328.883,89.125l107.59,107.589l-272.34,272.34L56.604,361.465L328.883,89.125z M518.113,63.177l-47.981-47.981 c-18.543-18.543-48.653-18.543-67.259,0l-45.961,45.961l107.59,107.59l53.611-53.611 C532.495,100.753,532.495,77.559,518.113,63.177z M0.3,512.69c-1.958,8.812,5.998,16.708,14.811,14.565l119.891-29.069 L27.473,390.597L0.3,512.69z"></path> </g> </g></svg>
</button>
Here is the css code of the class I want to add to the svg:
.pressed {
fill: #19ba00;
}
And lastly here is the javascript code for the function i want to add (the function contains code referring to a different element):
const tableCell = document.querySelector(".divTableCell");
const cellInput = document.querySelector(".cellInput");
const table = document.querySelector(".divTable");
const display = document.querySelector(".display");
const navButton = document.querySelector(".nav-button");
const addButton = document.querySelector("#control-btn-add");
const genButton = document.querySelector("#control-btn-generate");
const editButton = document.querySelector("#control-btn-edit");
const editButtonSvg = document.querySelector("#edit-svg");
editButton.addEventListener("click", () => {
let allCellInputs = document.querySelectorAll(".cellInput");
for (let i = 0; i < allCellInputs.length; i++) {
allCellInputs[i].toggleAttribute("disabled");
}
editButtonSvg.classList.toggle("pressed");
});
As you can see I’ve already tried using the classList.toggle() method but it doesn’t work. When I add the class to the html svg element manually, it’s also not working.
I’ve tried changing the class into a id and use the toggleAttribute() method but it didn’t work as well.
I’ve figured out though that when I’m using the setAttribute() method it works just fine, but I’m not getting the toggle effect I want.