Customizing the CiviContribute form through Drupal module
From what you describe, it sounds like you are seeing some of CiviCRM's contribution page JS kick in after your customised form is rendered.

It is probable that configuring your CiviCRM Contribution page to use the defaults you desire will resolve this issue. Your approach of setting the defaults should translate to setting the form values, which should then be respected on initial load, but ... that's not happening :)

Take a look at the JS inlined on a default CiviCRM contribute form, eg the CiviCRM Demo site's Member Signup and Renew form when configured to permit optional membership auto-renew.

If you inspect the code immediately after the auto_renew element, you'll see this block coming from CiviCRM's templates.

&lt;script type="text/javascript"&gt;
CRM.$(function($) {
    //if price set is set we use below below code to show for showing auto renew
    var autoRenewOption =  '0';
    $('#allow_auto_renew').hide();
    if ( autoRenewOption == 1 ) {
        $('#allow_auto_renew').show();
    } else if ( autoRenewOption == 2 ) {
        var autoRenew = $("#auto_renew");
        autoRenew.prop('checked',  true );
        autoRenew.attr( 'readonly', true );
        $('#allow_auto_renew').show();
    }
});
&lt;/script&gt;


That code is fairly straightforward to read - if auto_renew is set to 2 (from the contribute form settings, which allow you to configure this behaviour per contribute form), then the JS will check that box on page load, as you're seeing.

If I'm reading all that right, and with the caveat of not having seen your actual site at all, I think you can configure this from your contribute form settings for each of the form IDs listed above. The setting is on the "Membership" tab and looks like this:



The recurring defaults are tied to the settings in the Contribute form (and depend on settings in both Membership Type and Payment Processor config).

If that's not enough, you can override the JS by customising CiviCRM's templates, and you can add your own JS also if need be. That then would be your next path forward - modify that template variable based on the switch statement in your existing code.

You can certainly do it via JS though, as demonstrated by this customised contribute form (js here).