To be brief, I‘m looking for a way to reset my filter made on jquery. This is my html for searching, input text + button submit:
JavaScript
x
12
12
1
<label for="formInput">Rrecherche: </label>
2
3
<input class="form-control search-box__input"
4
name="formInput" type="text"
5
id="formInput" />
6
7
<input type="submit" id="btn-search" class="search-box__submit cc-btn" value="rechercher">
8
9
<button type="button" class="search-box__reset" id="search-box__reset">Réinitialiser</button>
10
11
12
Html table :
JavaScript
1
63
63
1
<table class="cc-table table table-bordered" id="cc-table">
2
<thead>
3
<tr>
4
<th>
5
<p> Candidat(e) </p>
6
</th>
7
<th>
8
<p> parrainages validés </p>
9
</th>
10
<th>
11
<p> 01/03/2022 </p>
12
</th>
13
<th>
14
<p> 03/03/2022 </p>
15
</th>
16
</tr>
17
</thead>
18
<tbody>
19
<tr style="">
20
<td>
21
<p> ZARTHAUD Nathalie </p>
22
</td>
23
<td>
24
<p> Mark </p>
25
</td>
26
<td>
27
<p> Otto </p>
28
</td>
29
<td>
30
<p> @mdo </p>
31
</td>
32
</tr>
33
<tr style="">
34
<td>
35
<p> ASSELINEAU François </p>
36
</td>
37
<td>
38
<p> Jacob </p>
39
</td>
40
<td>
41
<p> Thornton </p>
42
</td>
43
<td>
44
<p> @fat </p>
45
</td>
46
</tr>
47
<tr style="">
48
<td>
49
<p> HAMON Benoît</p>
50
</td>
51
<td>
52
<p>Larry the Bird </p>
53
</td>
54
<td>
55
<p> @twitter </p>
56
</td>
57
<td>
58
<p>Larry the Bird </p>
59
</td>
60
</tr>
61
</tbody>
62
</table>
63
This is my jquery to filter an html table :
……………………………………
……………………………………
……………………………………
JavaScript
1
7
1
$("#btn-search").on('click', function (e) {
2
var value = $("#formInput").val().toLowerCase();
3
$("#cc-table tbody tr").filter(function () {
4
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
5
});
6
});
7
Thank you
Advertisement
Answer
Simple, once the “reset” button is clicked, the value of the input will be changed to empty and starting the search will reset the filter.
JavaScript
1
10
10
1
$("#btn-search").on('click', function(e) {
2
let value = $("#formInput").val().toLowerCase();
3
$("#cc-table tbody tr").filter(function() {
4
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
5
});
6
});
7
$("#search-box__reset").on('click', function(e) {
8
let value = $("#formInput").val('').text('');
9
$("#btn-search").click();
10
});
JavaScript
1
73
73
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<label for="formInput">Rrecherche: </label>
3
4
<input class="form-control search-box__input" name="formInput" type="text" id="formInput" />
5
6
<input type="submit" id="btn-search" class="search-box__submit cc-btn" value="rechercher">
7
8
<button type="button" class="search-box__reset" id="search-box__reset">Réinitialiser</button>
9
10
11
12
<table class="cc-table table table-bordered" id="cc-table">
13
<thead>
14
<tr>
15
<th>
16
<p> Candidat(e) </p>
17
</th>
18
<th>
19
<p> parrainages validés </p>
20
</th>
21
<th>
22
<p> 01/03/2022 </p>
23
</th>
24
<th>
25
<p> 03/03/2022 </p>
26
</th>
27
</tr>
28
</thead>
29
<tbody>
30
<tr style="">
31
<td>
32
<p> ZARTHAUD Nathalie </p>
33
</td>
34
<td>
35
<p> Mark </p>
36
</td>
37
<td>
38
<p> Otto </p>
39
</td>
40
<td>
41
<p> @mdo </p>
42
</td>
43
</tr>
44
<tr style="">
45
<td>
46
<p> ASSELINEAU François </p>
47
</td>
48
<td>
49
<p> Jacob </p>
50
</td>
51
<td>
52
<p> Thornton </p>
53
</td>
54
<td>
55
<p> @fat </p>
56
</td>
57
</tr>
58
<tr style="">
59
<td>
60
<p> HAMON Benoît</p>
61
</td>
62
<td>
63
<p>Larry the Bird </p>
64
</td>
65
<td>
66
<p> @twitter </p>
67
</td>
68
<td>
69
<p>Larry the Bird </p>
70
</td>
71
</tr>
72
</tbody>
73
</table>