• Categories
    • PHP
    • phpMyAdmin
    • PHPMailer
    • FFMpeg
    • PHPEXcel
    • PHPDoc
    • PHPUnit
    • CakePHP
    • CakePHP 2.0
    • Cake PHP 2.1
    • CakePHP Model
    • Facebook PHP SDK
    • composer-php
    • PHP 7
    • PHP GD
    All Categories
  • About

How in Laravel run JavaScript code stored in php variable?

phplaravellaravel-livewire


PHP Snippet 1:

public $foo;

$this->dispatchBrowserEvent('custom-event', ['foo' => $this->foo]);

PHP Snippet 2:

<script>
    window.addEventListener('custom-event', event => {
          alert('here value foo: ' + event.detail.foo);
          console.log(event.detail.foo);
    })
</script>

PHP Snippet 3:

<script>
    document.addEventListener('custom-event', () => {
        {!! json_encode($script) !!}
    })
</script>

Related Snippets

How can I solve "laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system"?

How to override header set in Apache config with more specific header in a virtual host

How to install ZeroMQ for PHP on an Alpine Linux container?

Laravel 5.1 Unknown database type enum requested

Transpose and flatten two-dimensional indexed array where rows may not be of equal length

Laravel 5.1 how to use {{ old('') }} helper on blade file for radio inputs

Clients authentication and user authentication with laravel

Symfony Make:Migration : The metadata storage is not up to date, please run the sync-metadata-storage command to fix this issue

How to render html from a @foreach loop in a textarea

CSS file not imported in laravel blade view

curl returning 301 error after migrating to https

Reading input in php from terminal

Create a zip file and download it

Custom add to cart button URL for specific product in WooCommerce?

Getting public posts of a random user from Facebook API

About Contact Privacy policy Terms and conditions