• 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 each() function replacement

Adding one microsecond to Datetime object in PHP

Is it possible to change the table name in the migration file-laravel

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

Unable to match results of php hash_hmac() and coldfusion hmac()

Why i get wrong output for html markdown?

Show date difference as "20" instead of "20 years ago"

401 Unauthorized only occurring on some pages in Laravel 8

MOODLE: What does it mean to aggregate h5p assets?

Installing Composer - Internal Error

Required field only if another field has a value, must be empty otherwise

Cut an arabic string

Clients authentication and user authentication with laravel

add uri parameter to Route name in laravel

Use Python plotly chart in PHP

About Contact Privacy policy Terms and conditions