Get value from Javascript to Livewire Component

Could you tell me if it is possible to get the value of a Javascript field and pass it to a Laravel Livewire property:

First I load in an input with Javascript the following value:

document.getElementById('lat-span').value = (place.geometry['location'].lat());

This loads the input with the value, now I tried to retrieve that value with wire: model and it appears null, I understand that it is not done the way I had in mind:

<input  wire:model="latitud" value="lat-span" id="lat-span" />

How can I directly pass this javascript value to Livewire property?

Livewire.emit('getLatitudeForInput', place.geometry['location'].lat());

in component

public $latitud;
protected $listeners = [
     'getLatitudeForInput'
];
//
public function getLatitudeForInput($value)
{
    if(!is_null($value))
        $this->latitud = $value;
}

in balde just

<input  wire:model="latitud" id="lat-span" />

Hope this help you

It works perfect!! Thanks a lot!

1 Like

You might also be able to use this approach, which saves having a seperate livewire method;

2 Likes