build a conditional using object

Asked

Viewed 44 times

1

Is it possible to create a conditional by assigning the conditional within a value? I did it the way I did it, but it didn’t 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();
    }
}

o Jsfiddle: http://jsfiddle.net/vgh42jqb/

2 answers

1


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

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

He will assign to condicional the value of the valuation valor === "SIM"

Fiddle

0

That 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();
    }
}

Browser other questions tagged

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