• 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

Calculating Median of an array in PHP

User pool client {id}does not exist

Call authenticate manually in router middleware

Laravel post contact form giving me error 419

RuntimeException: Unable to create the cache directory (/var/www/sonata/app/cache/dev)

How to get an Array value inside an object in an array Php [duplicate]

get data-value with variable value

What is the function of the (new Date()).getTime() in PHP?

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

Passing a boolean value from checkbox in Laravel form

MySQL upgrade causing unexpected results on simple WHERE clauses

Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::fetch_assoc() [duplicate]

Laravel/docker-compose/redis - Class 'Redis' not found

CodeIgniter 4: Like and Dislike Functionality

Laravel 5.2: Unable to locate factory with name [default]

About Contact Privacy policy Terms and conditions