0

I request to the server with ajax and get a response some HTML code. I get it and add to div which has id='conten' but styles and scripts not apply

this is my ajax request tirgger:

  <li class="catalogs"  catalog_id="<?= $category->id?>"><?= $category->title?></li>

this is my ajax post:

//get catalogs
        $('.catalogs').click(function () {
            let catalog_id = this.getAttribute('catalog_id')
            console.log(catalog_id)

            $.ajax({

                url: '/site/catalog',
                type: 'get',
                data: {
                    id: catalog_id
                }
            }).done(function (response) {
                $('#conten').html(response);
            })

        })

this is my action in SiteController

    public function actionCatalog($id)
    {
        $products = \common\models\Product::find()->where(['category_id' => $id]);
        $subCategories = \common\models\SubCategory::findAll(['category_id' => $id]);
        return $this->renderAjax('index',[
            'products' => $products,
            'subCategories' => $subCategories,
        ]);
    }

and it is my div with id="conten"

 <div id="conten">
     <?= $content?>
 </div>

my AppAssets files

// css
'css/plugins.css',
        'css/style.css',
        'css/responsive.css'
//js

'js/jquery.js',
        'js/plugins.js',
        'js/functions.js'


Azam I.
  • 23
  • 10

1 Answers1

0

You load your JS and CSS with ajax? if so, this is because your html page where you include all the css are loaded as the document is ready..it reads all classes and according to the style sheet it applies on that.. but in ajax we load the other page on the same html page and this time browser not map the html classes to the style sheet that is why your css is not applying on that.

There are two ways to do this one is inline css and other one is load all the css files again after the ajax page is loded.. you can do it via jquery or javascript.

Here is the example: Is there an easy way to reload css without reloading the page?

Serghei Leonenco
  • 3,110
  • 2
  • 6
  • 13