Using confirm() javascript with checkbox

Asked

Viewed 328 times

1

I got this line that’s inside a foreach:

<input type="checkbox" id="idAprovacao" name="idAprovacao" 
onclick="atualizaSituacao(<?php echo $resulta->id_aprovacao;?>);"
<?php echo ($resulta->in_situacao == "4") ? "checked disabled" : "" ?> value="">

And I have this function that makes the question exist and if it is accepted does something and if it should not uncheck the checkbox, but this only happens for the first record displayed.

function atualizaSituacao(id)
{
    confirma = window.confirm('Esse registro realmente foi lançado no SIGRH?');

    var idAprovacao = id;

    if(confirma == true) {    

    // acontece algo

    } else {    
        document.getElementById('idAprovacao').checked = false;
    }
}

What’s wrong or missing?

1 answer

4


Theoretically, an ID should only belong to 1 element. You have several elements with id="idAprovacao".
The command document.getElementById returns only 1 element, the first one he finds.
Therefore, you should put a different ID for each checkbox.

In the imprint of input, change:
id="idAprovacao"
for:
id="idAprovacao<?php echo $resulta->id_aprovacao;?>".

And in javascript, change:
document.getElementById('idAprovacao').checked = false;
for:
document.getElementById('idAprovacao'+id).checked = false;

  • Bro, thank you so much, I knew it was something like that, I just wasn’t sure how to really do it. I spent time trying and decided to come here to ask. Thank you!

  • Trying to take advantage of the question now just to leave better what I want, as I leave so that when you mark also stay as disabled the same checkbox?

  • 1

    document.getElementById('idAprovacao'+id).disabled = true;

  • He was inventing hahaha thing, but he had the . disabled then.. now good. Thanks.! ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.