• 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

PHP Find Array Index value in multi-line array

Elastic Beanstalk with Laravel Envoy

PHP Form Not Inserting

Cross-Origin Resource Sharing (CORS) witth Laravel and Vuejs

Prevent blank space in pdf pages (DomPdf)

Limit login attempts in Laravel 5.7

Prestashop cUrl Login

How to install php_imagick on wamp64

Not able to override collapsible.js in magento 2

Symfony Error: "An exception has been thrown during the rendering of a template"

Respond with status code 401 on authentication failure using Laravel and Passport?

Change the alert text on add to cart action without selected variation in Woocommerce

PHP password_verify

How to add a sidebar to Woocommerce Shop Page?

Laravel whole batch is cancelled if one Job fails

About Contact Privacy policy Terms and conditions