Laravel livewire, How to include javascript file in blade

Viewed 32

I will try to avoid duplicate questions and be as clear as possible. I recently created a js script to do a filtered search of a table and I need this file to be included on multiple blades. The problem is that the script works if it is pasted into the blade, but I cannot include it properly into the blade, I tried to put it in the public folder and install mix, unsuccessful in both cases. I'm sure that the problem is the folder where i put the js file and how i try to include it, but i didn't find a solution even though I tried in every way.

This is my js file(filtering_script.js):

 //ricerca filtrata tra le tabelle

function Filtering() {
// Dichiara le variabili
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");


for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
  txtValue = td.textContent || td.innerText;
  if (txtValue.toUpperCase().indexOf(filter) > -1) {
    tr[i].style.display = "";
  } else {
    tr[i].style.display = "none";
  }
}
}
}

I know it's probably a stupid problem but i can't really solve it, hope someone helps me, thank you.

1 Answers

Apparently the question is not appreciated, but i think it might be useful to some so i will post the solution for people starting with laravel. If you use livewire you should remember NOT to use npm run dev, but use npm run build, once done create two folders, 'css' and 'js' inside the public folder, then put your file here so they will be accessible to the rest of the components. The code to put inside the blade, for javascript, is:

<script src="{{url('js/yourfile.js')}}"> </script>
Related