Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LaraBABA's avatar

Live wire how to click, set a variable and click submit button at once?

Hi all,

I am trying to set x-on:click="addSearch = 'words here'" on the input field when the div is clicked. But at the same time i would like the search bar to submit the value. I can set the addSearch = 'words here'" in the search bar when clicking on the div, but I cannot manage to get the type submit to be simulated(eg: After having the word in the search bar, we submit the value to the laravel back end).

//Search Bar
<x-inputs.text name="search" x-model="addSearch" value="{{ $search ?? '' }}" autocomplete="off"></x-inputs.text>

//Div block
<div class="border-dashed border-r-2 p-2 shadow-sm" x-on:click="addSearch = 'words here'">

//Button
<button  type="submit" class="button button-primary" ></button>
</div>

Any idea please?

Thanks

0 likes
6 replies
MohamedTammam's avatar

Is it Livewire or Alpine?

If Livewire, you can use $wire.set

<div class="border-dashed border-r-2 p-2 shadow-sm" x-on:click="$wire.set('addSearch', 'words here') ">
LaraBABA's avatar

@MohamedTammam Thanks for the reply. I am using Livewire in the app, but this is not a livewire component, I need to do it through "Alpine.js". I tried this: x-on:click="addSearch = 'word here';document.getElementById('submit-button').click()"

But the click comes at the same time than the addSearch = 'word here

LaraBABA's avatar

I think I need something along these lines:

$dispatch('button', 'word here')

But how to click that "button " on dispatch

LaraBABA's avatar

I tried with this:

x-on:click="addSearch = 'word here';$refs.submit.click()"

But I need some kind of delay between the addSearch = '' and $refs.submit.click() as it is too quick. I would have done this easy with vue but not with livewire

LaraBABA's avatar

Even this does not work...running out of ideas:

x-on:click="addSearch = 'word here';setTimeout($refs.submit.click(), 2000)"
LaraBABA's avatar
LaraBABA
OP
Best Answer
Level 10

Finally!!!

x-on:click="addSearch = 'words here';setTimeout(() => $refs.submit.click(), 3000)"
1 like

Please or to participate in this conversation.