jQuery: alternar no se activa al recargar la página

Tengo este HTML.

<tr>
    <td width="75">Mon</td>
    <td class="bn_time" width="60">
        <input id="mon_open" name="mon_open" class="time" placeholder="opening time" />
    </td>
    <td class="bn_time" width="10" align="center">-</td>
    <td class="bn_time" width="100">
        <input id="mon_close" name="mon_close" class="time" placeholder="closing time" />
    </td>
    <td colspan="3" align="center" class="bn_holiday" hidden>Holiday</td>
    <td>
        <input type="checkbox" name="mon_closed" id="mon_closed" class="bn_closed" />
        <label for="mon_closed">Closed</label>
    </td>
</tr>

i am using this jQuery.

$('.bn_closed').on('change', function() {
    var element = $(this);
    var id = element.attr('id');
    var day = id.slice(0,-7);
    var day_open = day+'_open';
    var day_close = day+'_close';
    var checked = element.is(':checked');
    var td = element.closest("td");
    td.siblings(".bn_time").toggle(!checked);
    td.siblings(".bn_holiday").toggle(checked);
});

to achieve the following.

if($('.bn_closed').is(':checked')) {
     //hide all <td> element with class bn_time
     //show <td> element with class bn_holiday
} else {
    //show all <td> element with class bn_time
    //hide <td> element with class bn_holiday
}

this works with change event of .bn_closed however when the page is reloaded and if the element with class .bn_closed is checked. the toggling does not comes into effect. what i want is the toggling should take place in the event of page reload too. how do i go with it?

gracias

preguntado el 26 de agosto de 12 a las 08:08

1 Respuestas

Prueba esto:

$('.bn_closed').on('change', function() {
    // ...
}).change()

Respondido 26 ago 12, 08:08

thank you, it is working, so what exactly does .change() does in here? - Ibrahim Azhar Armar

@IbrahimAzharArmar You are welcome, it just triggers the change event. - Carnero

okay so you mean, it triggers the change event on page load, instead of waiting for user to trigger the event. - Ibrahim Azhar Armar

@IbrahimAzharArmar Yes, it does so. - Carnero

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.