0

So if i have a small layout stored in example.blade.php, How do i use it in jquery ?

I want to include certain elements such as textboxes when a particular radio button is checked otherwise not.

Example:

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

    if ($("#role").prop( "checked")) {
        $("#content").html(@include('layouts.nav'));
    }
    else
    {
        $("#content").html('');
    }

});

The above code does not work so please provide some solution.

Raj
  • 1,928
  • 3
  • 29
  • 53
  • 2
    You should [never mix JS and PHP](https://github.com/alexeymezenin/laravel-best-practices#do-not-put-js-and-css-in-blade-templates-and-do-not-put-any-html-in-php-classes). Instead, use Ajax to update HTML or render it as hidden HTML and put it to `#content` dynamically. – Alexey Mezenin Dec 26 '17 at 13:51
  • $(function(){ $("#includedContent").load("b.html"); }); check here https://stackoverflow.com/a/38850417/5700401 – Abhijit Jagtap Dec 26 '17 at 14:26

2 Answers2

0

You can not do that. @include() is Blade and will be interpreted by PHP while JS runs in the browser. The Blade directives are already interpreted by PHP when the code hits the browser

common sense
  • 3,775
  • 6
  • 22
  • 31
0

You can create a hidden div in your view and then use jquery to get it's html content, here is an example:

<div class="layouts-nav" style="display:none;">
    @include('layouts.nav')
</div>

<script>

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

        if ($("#role").prop( "checked")) {

            var layoutNav = $('#layouts-nav').html();
            $("#content").html(layoutNav);
        }
        else
        {
            $("#content").html('');
        }

    });

<script>
YouneL
  • 8,152
  • 2
  • 28
  • 50