Calling jquery function on document ready and on on.change

I just updated this function from using live to on method. Below is my code:

$(document).ready(function($){
  function toggle_metaboxes() {

    var format = $('#post-formats-select input[type="radio"]:checked').val();

    $('#orn_metabox_gallery').fadeOut('fast');
    $('#orn_video_link').fadeOut('fast');

    if ('gallery' == format) {
      $('#orn_metabox_gallery').fadeIn('slow');
    } else if ('video' == format) {
      $('#orn_video_link').fadeIn('slow');
    }
  }
  toggle_metaboxes(); // I am calling this function once on document ready
  $(document).on('change', '#post-formats-select input[type="radio"]', toggle_metaboxes)
             .filter(':checked')
             .trigger('change');
});

My Question: Is there any better way of doing this? I am calling the function on document ready and then when radio changes value.

Related posts

Leave a Reply

1 comment

  1. You can assign multiple events like this:

    $(document).on('ready change', '#post-formats-select input[type="radio"]', toggle_metaboxes).filter(':checked').trigger('change');