wertyu
wertyu

Reputation: 121

wire:change on select in livewire

I want to trigger a function in livewire when an option is selected in dropdown. But with wire:click or wire:change dd in function not working. Is there a wire:bla bla for this in livewire?

<x-select name="course_id" label="{!! __('Ders') !!}" wire:model="course_id" wire:change="courseSelected" id="course_id"  :options="$this->courses"/>
 public function courseSelected(){
        dd("here");
}

Upvotes: 1

Views: 8515

Answers (2)

Prospero
Prospero

Reputation: 2352

if you bind the element to property using wire:model, you can hook the lifecycle as mentioned above

public function updatedCourseId($value)
{
    dd($value);
}

in a different approach, you can listen for the event using wire:change, and for that, you must define the event in listener property like

<x-select label="{!! __('Ders') !!}" wire:change="$emit('courseSelected', $event.target.value)" id="course_id"  :options="$this->courses"/>

// in component
protected $listeners = [
   'courseSelected'
];

public function courseSelected($value)
{
   dd($value);
}

Upvotes: 2

fotrino
fotrino

Reputation: 21

you should hook to the updated method in the lifecycle.

https://laravel-livewire.com/docs/2.x/lifecycle-hooks

public function updatedCourseId(){
   dd("here");
}

Also remove wire:change

Upvotes: 1

Related Questions