build a conditional using object

1

Is it possible to create a conditional by assigning the conditional within a value? I did it the way it did, but it did not work:

HTML

<input type="radio" name="pg1" value="SIM"> TESTE <BR/>
<input type="radio" name="pg1" value="NÃO"> TESTE 2

<div id="esconde">
    <p>NADA</p>
</div>

JAVASCRIPT

$(document).ready(function(){
    $("[name='pg1']").click(function(){
        var valor = $(this).val();

        divShowOrHide({
            //valorEscolhido: valor,
            condicional: valor+" === SIM",
            hideDiv: "#esconde"
        });
    });

});
function divShowOrHide(obj){
    //alert(obj.condicional);
    if(obj.condicional){
        $(obj.hideDiv).show();
    } else {

        $(obj.hideDiv).hide();
    }
}

Jsfiddle: link

    
asked by anonymous 23.10.2015 / 17:56

2 answers

1

In case of a true / false, you can do something like:

divShowOrHide({
    //valorEscolhido: valor,
    condicional: valor === "SIM",
    hideDiv: "#esconde"
});

It will assign to condicional the evaluation value valor === "SIM"

Fiddle

    
23.10.2015 / 18:01
0

This would be an alternative

divShowOrHide({
    //valorEscolhido: valor,
    condicional: "'"+valor+"'"+" === 'SIM'",
    hideDiv: "#esconde"
});

function divShowOrHide(obj){
    //alert(obj.condicional);
    if(eval(obj.condicional)){
        $(obj.hideDiv).show();
    } else {

        $(obj.hideDiv).hide();
    }
}
    
23.10.2015 / 19:19