• 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

Unable to create lockable file - Laravel 8 & PHP 8

Installing Composer - Internal Error

How to set time with DateTime modify?

oauth-private.key does not exist or is not readable

Converting alphabet letter to alphabet position in PHP [duplicate]

How to get array values using foreach in laravel

Image upload not working through ajax Laravel

Laravel/ PHP: Order By Alphabetical with numbers in order [duplicate]

How can I access an array/object?

How can I get a div content in php

How to get monthly wise data in laravel

How to solve Duplicate entry '0' for key 'PRIMARY' in Wordpress?

Get data from accuweather api url

Batch request Google Calendar php API

How to Mock the Request Class in Laravel?

About Contact Privacy policy Terms and conditions