6

I have a text input field which works great if I update it myself when typing inside it, however, what I need is to have this input field be populated with data from javascript, rather than the user. This is my input field

<input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="images" wire:model="images">

This is what happens if I type "123" in the input field, I get this for the input field's value:

enter image description here

However when I update it using javascript, document.getElementById("images").value = "Hello" The input field is populated with the new data: enter image description here

But it doesn't make a new fetch call to get the data and the last one is with the "123" data that was inserted without javascript...

enter image description here

Any idea of how it can get the data after updating the input's value from javascript?

4 Answers 4

15

After changing the value of the input using javascript, you can trigger the input event to make livewire update the model.

document.getElementById("images").value = 'Hello';
document.getElementById("images").dispatchEvent(new Event('input'));

If you're using wire:model.lazy, you should use a 'change' event instead of 'input'

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks bro, i am very new to livewire, in fact don't like it but have some task to do, I was stuck in the problem, tried a lot of ways, then find your suggestion and it work thank you
Worked like a charm with a change event for a radio button. Thanks!
5
<script>
        document.addEventListener('livewire:load', function () {
           @this.set('images','Hello');
           //console.log('Hello');
        });
</script>

Make a public property name 'images, initiate it in the mount. I hope it will solve your problem. Let me know.

Comments

2

You can populate the input field using inline scripts in livewire like:

document.addEventListener('livewire:load',function ()
        {
            @this.images = "Hello";
        });
<input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="images" wire:model="images">

3 Comments

where should I add this event listener?
In your livewire blade file ... for reference: laravel-livewire.com/docs/2.x/inline-scripts
is there a way to make the change without triggering a livewire update?
1

Add wire:ignore on parent element of the input field

Like

<div wire:ignore>

   <input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="images" wire:model="images">

</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.