• 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

Match csv filenames to table names and import

array_key_exists(): The first argument should be either a string or an integer

"Mixed content blocked" when running an HTTP AJAX operation in an HTTPS page

protect images from being copied

Multiply each value in array using array_map function

Reducing authentication calls on external API (Laravel 5.6)

Call to a member function fetch_field() on a non-object MySQLiDB

Laravel csrf token mismatch for ajax POST Request

Yii2: How to download backup files using spanjeta/yii2-backup?

ldap is missing from system when installing adldap2 in laravel

I need to link Google Sheet with my Laravel

Yii Ajax Submit button

How can I resolve "Your requirements could not be resolved to an installable set of packages" error?

How to pass data to all views in Laravel 5?

Laravel eloquent update record without loading from database

About Contact Privacy policy Terms and conditions