2

I am using Laravel 5 where I can make a master page (header, footer) and import every page's content in that master page.

I have a dropdown in my header (master page) and in javascript, I have a variable connected to it - var clickable = true;.

//Master Blade
@yield('content')

<script>
   var clickable = true;    

   $('.dropdown).click(function() {
      clickable = false;
   })';
</script>

In my view (content), I have a function that I also want to change clickable = true;

//View
   ('.contentElement').click(function() {
       clickable = true;
   });

However, it doesn't recognise it. Is there a way that I can achieve what I want to?

senty
  • 12,385
  • 28
  • 130
  • 260

2 Answers2

1

Most likely you are referencing a variable that is in a different scope. It is hard to tell if that's the case with your minimal code provided but most likely this is why your variable is not being recognized.

You should not be writing JavaScript in different parts of your template, it will make for hard to maintain code. Instead, put all your JavaScript code in its own .js file and include it in your page.

There are many benefits of doing this, but mostly it will help you better structure your code and save pointless debugging time.

Community
  • 1
  • 1
samrap
  • 5,595
  • 5
  • 31
  • 56
0

I found my answer. I created

<body>
  <script>
      var clickable = false;
  </script>
    ....
  @yield('content')
  <script> /* rest of JS */ </script>
</body>
senty
  • 12,385
  • 28
  • 130
  • 260