So i wanna make a search bar that searching data with jquery, then after data was found i replace into the 'div' by not refreshing a page. Then when i wipe all character in search bar it will reset the old div. After that my JS function wont work again until i reload the page.
Where is the problem i cant find it out, for the record i am using :
- JQUERY 3.4.1
- js.js file
- search.js file
I am not mixturing js.js with the search.php because js.js containing vanilla js and search.js was jquery file.
Search Bar
<!-- The search menu -->
<form method="GET">
<div class="input-group">
<input type="text" name="search" id="cariProjek" class="form-control search-input" placeholder="Cari projek ...">
</div><br>
</form>
The div
<div id="reset">
<div id="search">
<table>
<tr>
<td>
<label for="tempFolderPath" class="form-label"><b>Temp Folder Lokasi</b></label>
</td>
<td>:</td>
<td>
<input type="text" class="form-control" name="tempFolderPath" id="tempFolderPath" placeholder="Ini readonly!!!" readonly></label>
</td>
</tr>
<tr>
<td>
<label for="namaFileBaru" class="form-label"><b>Nama File Baru</b></label>
</td>
<td>:</td>
<td>
<input type="text" class="form-control" name="namaFileBaru" id="namaFileBaru" autocomplete="" required>
</td>
</tr>
<tr>
<td>
<label for="namaFileLama" class="form-label"><b>Nama File yang akan Diubah</b></label>
</td>
<td>:</td>
<td>
<input type="file" class="form-control" id="namaFileLama" multiple onchange="showname()" required>
</td>
</tr>
<tr>
<td>
<label for="tempNama" class="form-label"><b>Temp Nama File</b></label>
</td>
<td>:</td>
<td>
<input type="text" class="form-control" name="tempNama" id="tempNama" readonly>
</td>
</tr>
</table>
<button type="button" name="submit" class="btn btn-primary" id="submitForm">Selesai</button><br><br>
<table>
<tr>
<td>
<label for="folderPath" class="form-label"><b>Folder Lokasi Asal</b></label>
</td>
<td>:</td>
<td>
<input type="text" class="form-control" style="width: 300px;" name="folderPath" id="folderPath" autocomplete="" placeholder="Pertama input ini dulu baru Simpan" require>
</td>
</tr>
</table>
<button type="button" name="submitFolderPath" class="btn btn-success" id="submitFolderPath">Simpan</button>
<button type="button" name="resetFolderPath" class="btn btn-danger" id="resetFolderPath">Reset</button>
</div>
js.js (This code wont work, after the div was reset)
Edit : The main issue is was here.
let submitFolderPath = document.getElementById('submitFolderPath');
let resetFolderPath = document.getElementById('resetFolderPath');
let folderPath = document.getElementById('folderPath');
let tempFolderPath = document.getElementById('tempFolderPath');
submitFolderPath.addEventListener('click', function(){
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200) {
let date = new Date();
date.setDate(date.getDate() + 1);
const expires = "expires=" + date;
document.cookie = "path=" + folderPath.value + "; " + expires + "; path=/";
tempFolderPath.value = folderPath.value;
}
}
xhr.open('GET', 'index.html', true);
xhr.send();
});
resetFolderPath.addEventListener('click', function(){
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200) {
let date = new Date();
date.setDate(date.getDate() - 1);
const expires = "expires=" + date;
let cookie = document.cookie = "path=" + folderPath.value + "; " + expires + "; path=/";
folderPath.value = '';
tempFolderPath.value = folderPath.value;
}
}
xhr.open('GET', 'index.html', true);
xhr.send();
});
search.js (Pretty sure the problem its here)
Edit : Actually the problem is not here
$(document).ready(function(){
$('#cariProjek').on('keyup', function(){
if($('#cariProjek').val() == ''){
$("#reset").load(location.href+" #reset>*","")
}else{
$('#search').load('search.html?keyword=' + encodeURIComponent($('#cariProjek').val()));
}
})
});
search.html (only testing)
<h1>Test</h1>