JSFiddle - React, Tailwind, and code Playground
HTML
<input type="radio" name="fpago" id="fpago1" value="1" />one
<input type="radio" name="fpago" id="fpago2" value="2" />two
<br>
<span id="myspan" >bla bla</span>
<input type="hidden" id="id_grupo" value="1" />
JavaScript
$(document).ready(function() {
$("#myspan").hide();//hide it by default
$("#fpago1, #fpago2").bind('change click',function(){
togglePlazo();
});
//in this case, id_grupo is "1" so shuld check the radio AND show the span...
grupo = $("#id_grupo").val();
if(grupo != '0'){
$("#fpago2").attr('checked', true); //<-- it does check it but it doesn't trigger the function (i.e. the span doesn't show)
//of course i could just add a ".click()" to my selector to ensure to trigger, but it should not be necesary, since I already bound it .
}
});
function togglePlazo(){
plazo = $("[name='fpago']:checked").val();
//alert(plazo);
if(plazo == "2"){
$("#myspan").show();
}else{
$("#myspan").hide();
}
}