Way to add captions to gallery images by code

you can change caption programmatically by jquery something like this:

( function( $ ) {    
    function function_name() {
          $( '#gallery-<gallery_ID>-<image_ID>').text("new caption");
}

function_name();
} )( jQuery );