Input hidden control checkbox

0

Is it possible to have an event that, depending on the change of a hidden input, does certain actions?

I've done this for a checkbox , but I'm not able to do the reverse ...

function getBitCheckbox(checkboxElem) {
    console.log(checkboxElem.id);

    if (checkboxElem.id == 'checkbox_bitInativo') {
        if (checkboxElem.checked) {
            // set bit Anulado a true
            $("#bitInativo").val(1);
        } else {
            //set bit Anulado a false
            $("#bitInativo").val(0);
        }
    }

    if (checkboxElem.id == 'checkbox_bitAprovado') {
        if (checkboxElem.checked) {
            // set bit Aprovado a true
            $("#bitAprovado").val(1);
        } else {
            //set bit Aprovado a false
            $("#bitAprovado").val(0);
        }
    }
}
    
asked by anonymous 29.10.2018 / 17:02

1 answer

1

I do not know if I understand the question well, but you want to know how to automatically activate an event when changing the value of an input via script, is that it? Of course JavaScript does not activate any event, but you can call this event right after changing the value, for example:

I set here the element that invokes foo when its value is changed -

<input id="bitAprovado" type="hidden" onchange="foo(this)">

So to activate the same event by changing the value via script, I use -

$("#bitAprovado").val(1).change();

- Edit -

There is a second alternative called MutationObserver, which is used to register changes by the script

let bitAprovado = document.getElementById('bitAprovado');
let observer = new MutationObserver(mutations => foo(mutations[0].target));
observer.observe(bitAprovado, {attributes: true});

In this case the event would only activate when changing the value via script. There is also no need to call .change () after changing the value.

    
29.10.2018 / 17:57