|
5 | 5 | var $ts = $(elem); |
6 | 6 | var tableId = $ts.next().attr('id'); |
7 | 7 | var inputId = 'searchtable-' + i; |
8 | | - $ts.before('<label for="' + inputId + '"><strong>Enter keyword to search </strong></label><input id="' + inputId + '" type="search" autosave="csrsearch" results="1" placeholder="search" name="s">'); |
9 | | - $('#' + inputId).keyup(function () { |
10 | | - searchTable($(this).val(), tableId); |
| 8 | + var clearId = 'clear-' + inputId; |
| 9 | + $ts.before('<div style="position: relative; width: fit-content;"><label for="' + inputId + '"><strong>Enter keyword to search </strong></label><input id="' + inputId + '" autosave="csrsearch" results="1" placeholder="search" name="s"><a href="#" id="' + clearId + '" class="search-clear" style="display:none; position: absolute; right: 5px; top: 2px;"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 34 34" class="svg-icon x-skinny" width="10"><use fill="#000" xlink:href="/sites/default/files/svg/sprite_8.svg#x-skinny"></use></svg><span style="position:absolute; left:-10000px; top:auto; width:1px; height:1px; overflow:hidden;">Clear</span></a></div>'); |
| 10 | + |
| 11 | + var $input = $('#' + inputId); |
| 12 | + var $clear = $('#' + clearId); |
| 13 | + |
| 14 | + $input.keyup(function (e) { |
| 15 | + var val = $(this).val(); |
| 16 | + |
| 17 | + // Check if Escape key was pressed |
| 18 | + if (e.key === 'Escape' || e.keyCode === 27) { |
| 19 | + $(this).val(''); |
| 20 | + searchTable('', tableId); |
| 21 | + $clear.hide(); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + searchTable(val, tableId); |
| 26 | + $clear.toggle(val.length > 0); |
| 27 | + }); |
| 28 | + |
| 29 | + $clear.click(function (e) { |
| 30 | + e.preventDefault(); |
| 31 | + $input.val(''); |
| 32 | + searchTable('', tableId); |
| 33 | + $clear.hide(); |
| 34 | + $input.focus(); |
11 | 35 | }); |
12 | 36 |
|
13 | 37 | if ( $( ".page-node-1485" ).length ) { |
|
16 | 40 | $ts.prev('.cat_search').change(function () { |
17 | 41 | var selection = this.value; |
18 | 42 | if (selection) { |
19 | | - $('#' + inputId).val(selection); |
| 43 | + $input.val(selection); |
20 | 44 | searchTable(selection, tableId); |
| 45 | + $clear.show(); |
21 | 46 | } else { |
22 | | - $('#' + inputId).val(''); |
| 47 | + $input.val(''); |
23 | 48 | searchTable(selection, tableId); |
| 49 | + $clear.hide(); |
24 | 50 | } |
25 | 51 | }).change(); |
26 | 52 | } |
|
0 commit comments