• 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

Boolean assignment operators in PHP

Laravel Livewire: jQuery not working in child component loaded via @if statement

How to fix " Uncaught TypeError: Argument 1 passed to JpGraphException::defaultHandler() " problem in php 7.1.3 and jpgraph 4.2.6?

Display Brand Name Above Product Title in Woocommerce Cart, Checkout Page, Orders and Email Notification

"cannot list resources" error from WooCommerce REST API

Get sum of arrays inside array

Wordpress 'post_type_link' hides permalink

Parameter is not sent to Laravel route in Ajax

Laravel Carbon throws error The format separator does not match

Select option from dropdown menu with PHP and mySql

Elastic Beanstalk with Laravel Envoy

best way to store error messages in Laravel (session or variable)

Make survey at laravel 5.4 and MySQL

How to create a scheduler application in php

dockerizing Laravel + vue

About Contact Privacy policy Terms and conditions