Using confirm () javascript with checkbox

1

I have this line that is 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 causes the question to exist and if it is accepted it does something and should NOT clear the checkbox, however 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?

    
asked by anonymous 03.10.2014 / 21:45

1 answer

4

In theory, an ID should only belong to 1 element . You have multiple elements with id="idAprovacao" . The document.getElementById command returns only 1 element , the first thing it finds. Therefore, you should enter a different ID for each checkbox.

When printing input , change:% id="idAprovacao"
by: id="idAprovacao<?php echo $resulta->id_aprovacao;?>" .

And in javascript, switch:% document.getElementById('idAprovacao').checked = false;
by: document.getElementById('idAprovacao'+id).checked = false;

    
03.10.2014 / 21:54