-1
This is the code working without changing color:
<!DOCTYPE html>
<html lang='pt-br'>
<body>
<form>
<button type="button" class="btn glyphicon glyphicon-plus" value="botao ligado" onclick="mostrar(this)">
<p>botao ligado</p>
</button>
</form>
<script>
const button = document.querySelector('button');
const paragraph = document.querySelector('p');
button.addEventListener('click', updateButton);
function updateButton() {
if (button.value === 'botao desligado') {
button.value = 'botao ligado';
paragraph.textContent = 'botao ligado';
} else {
button.value = 'botao desligado';
paragraph.textContent = 'botao desligado';
}
}
function mostrar(e) {
if (e.classList.contains("glyphicon-plus")) {
e.classList.remove("glyphicon-plus");
e.classList.add("glyphicon-minus");
$cor = "btn-success";
} else {
e.classList.remove("glyphicon-minus");
e.classList.add("glyphicon-plus");
}
}
</script>
</body>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>
</html>
I tried to put a variable with the color of the button but it does not change. So:
<button name="referencia" type="button" class="btn <?php echo ($cor);?> glyphicon glyphicon-plus" value="botao ligado" onclick="mostrar(this)">
<p>botao ligado</p>
</button>
<script>
const button = document.querySelector('button');
const paragraph = document.querySelector('p');
button.addEventListener('click', updateButton);
function updateButton() {
if (button.value === 'botao desligado') {
button.value = 'botao ligado';
paragraph.textContent = 'botao ligado';
$cor="btn-success";
} else {
button.value = 'botao desligado';
paragraph.textContent = 'botao desligado';
$cor="btn-danger";
}
}
function mostrar(e) {
if (e.classList.contains("glyphicon-plus")) {
e.classList.remove("glyphicon-plus");
e.classList.add("glyphicon-minus");
} else {
e.classList.remove("glyphicon-minus");
e.classList.add("glyphicon-plus");
}
}
</script>
My colleague can do this with input instead of the button
<input type="button" id="button" value="button" style="color:white" onclick="setColor('button', '#101010')" ; />
<script>
var count = 1;
function setColor(btn, color) {
var property = document.getElementById(btn);
if (count == 0) {
property.style.backgroundColor = "green"
count = 1;
} else {
property.style.backgroundColor = "red"
count = 0;
}
}
</script>
Please clarify your specific problem or provide Additional Details to Highlight Exactly what you need. As it’s Currently Written, it’s hard to Tell Exactly what you’re asking.
–