Styles Not Loading When Dynamically Loading Content via AJAX in Bricks

I’m new to bricks and wordpress so I apologize if I am doing something wrong. I am trying to load different sections of my website dynamically using AJAX. It’s a customer portal using the wordpress my account pages. I’ve also created some custom endpoints. I have 4 buttons and when one is pressed, the content is displayed underneath properly but the styles do not apply when loaded via ajax. Also it seems like other shortcodes from other plugins seem to be loading in fine with styles.

Here is my AJAX JavaScript code:

jQuery(document).ready(function($) {
    $('.dynamic-content-button').on('click', function() {
        var contentId = $(this).data('content-id');  

        $.ajax({
            url: ajax_object.ajax_url,
            type: 'GET',
            data: {
                action: 'load_dynamic_content',
                content_id: contentId
            },
            success: function(response) {
                $('#dynamic-content-area').html(response);

                if (window.bricksFrontend) {
                    window.bricksFrontend.init();
                }
            },
            error: function() {
                alert('Sorry, there was an error loading the content.');
            }
        });
    });
});

This is what I have in my functions.php

function enqueue_dynamic_content_script() {
    wp_enqueue_script( 'dynamic-content', get_template_directory_uri() . '/js/dynamic-content.js', array('jquery'), null, true );
    
 
    wp_localize_script( 'dynamic-content', 'ajax_object', array(
        'ajax_url' => admin_url( 'admin-ajax.php' ),
    ));
}
add_action( 'wp_enqueue_scripts', 'enqueue_dynamic_content_script' );


function load_dynamic_content() {
    if ( isset( $_GET['content_id'] ) ) {
        $content_id = sanitize_text_field( $_GET['content_id'] );
        
        switch ( $content_id ) {
            case 'edit-address':
         
                echo do_shortcode( '[bricks_template id="285"]' ); // Replace with your template ID for Edit Address page
                break;

            case 'marketplace':
     
                echo do_shortcode( '[bricks_template id="414"]' ); // Replace with your template ID for Marketplace page
                break;

  
              case 'orders':
                echo do_shortcode( '[bricks_template id="281"]' ); 
                break;
				
			    case 'aktualnosci':
                echo do_shortcode( '[bricks_template id="430"]' ); 
                break;
				
				case 'edit-profile':
                echo do_shortcode( '[bricks_template id="1270"]' ); 
                break;
            
            default:
          
                echo 'Content not found.';
        }
    }
    wp_die();
}
add_action( 'wp_ajax_load_dynamic_content', 'load_dynamic_content' );
add_action( 'wp_ajax_nopriv_load_dynamic_content', 'load_dynamic_content' );

It shows up properly when visiting the page directly:

But this is what happens when it’s loaded dynamically.

Still can’t figure this out.

well because only content getting loaded not the head scripts

I would use htmx and head extension with it for simple ajax

use chatgpt to write a simple html example for you it is much cleaner way to create ajax if you need to.

@sinanisler Thank you for your response. I’ve tried implementing that but still am not getting any styles. This is my first website so I’m following some tutorials and using chatgpt for some explanations. Content loads in fine but seems like the head scripts are not.

My button is set up:
hx-get: /my-account/marketplace
hx-target: #account-content
hx-swap: outerHTML
hx-trigger: click

function bt_add_htmx_support() {
    wp_enqueue_script('htmx', 'https://unpkg.com/htmx.org@1.9.10', [], '1.9.10', true);
    wp_enqueue_script('htmx-head', 'https://unpkg.com/htmx.org/dist/ext/head-support.js', ['htmx'], '1.9.10', true);
}
add_action('wp_enqueue_scripts', 'bt_add_htmx_support');

function bt_is_htmx_request() {
    return isset($_SERVER['HTTP_HX_REQUEST']) && $_SERVER['HTTP_HX_REQUEST'] === 'true';
}

function bt_add_marketplace_to_menu($items) {
    $items['marketplace'] = __('Marketplace', 'bricks');
    return $items;
}
add_filter('woocommerce_account_menu_items', 'bt_add_marketplace_to_menu');

function bt_register_marketplace_endpoint() {
    add_rewrite_endpoint('marketplace', EP_ROOT | EP_PAGES);
}
add_action('init', 'bt_register_marketplace_endpoint');

function bt_marketplace_content() {
    if (bt_is_htmx_request()) {
        ob_start();
        ?>
        <head hx-head="merge">
            <style><?php echo file_get_contents(get_stylesheet_directory() . '/style.css'); ?></style>
        </head>
        <?php
        echo do_shortcode('[bricks_template id="281"]');
        $content = ob_get_clean();
        echo $content;
        exit;
    }
    echo do_shortcode('[bricks_template id="281"]');
}
add_action('woocommerce_account_marketplace_endpoint', 'bt_marketplace_content');

function bt_add_htmx_head_support() {
    if (is_account_page()) {
        ?>
        <script>
            document.addEventListener('DOMContentLoaded', () => {
                document.body.setAttribute('hx-ext', 'head-support');
            });
        </script>
        <?php
    }
}
add_action('wp_footer', 'bt_add_htmx_head_support');