0

I have a very simple problem. Whenever I click on div I want the data binding to be applied to the file input.

<div>click me</div> <input type="file" wire:model="photo"> <!-- while clicking on div, it should do the data binding to this file input --> 

I have been searching for it in the documentation but couldn't find any clue. Is it possible to do that?

3
  • you want to open the input file when you click on the div? Commented Jan 10, 2023 at 11:36
  • @haruk1515 Exactly Commented Jan 10, 2023 at 11:41
  • you can do the same with label right ? Commented Jan 10, 2023 at 13:54

1 Answer 1

2

Okei i was searching and testing, to see if i can acomplish that, and there is a solution with jquery:

<div id="trigger">click me</div> <input type="file" id="file_input" wire:model="photo"> $("#trigger").unbind("click").bind("click", function () { $("#file_input").click(); }); 

i will give credit where i found the answer:

triggering a file input button

if your idea is to use livewire only, you could create a function

public function fileTrigger(){ $this->dispatchBrowserEvent('TriggerFilem'); } 

add this to js:

window.addEventListener('TriggerFilem', e => { $("#trigger").unbind("click").bind("click", function () { $("#file_input").click(); }); }); 

and in the view:

<div wire:click="fileTrigger()" id="trigger">click me</div> <input type="file" id="file_input" wire:model="photo"> 

Edit:

If you don't want to use jquery, i was testing a solution on js only:

document.getElementById('trigger').addEventListener("click",() => { document.getElementById('file_input').click(); }); 

hope it helps!

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

3 Comments

It is a good solution but I want it without jQuery. Even in second solution the 'jQuery' is being used
@ZainFarooq i added another solution for js only, without jquery, hope it helps
I try that. I hope it works

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.