Activate DataTable highlighting and filtering based on input change

Description

I currently have a table in my HTML code that looks like this:

<table id="account-table" class="table display nowrap" cellspacing="0" width="100%">
    <thead>
    <tr class="text-left">
      <th>ID</th>
      <th>Type</th>
      <th>Name</th>
      <th>Email Address</th>
      <th>Action</th>
    </tr>
    </thead>
</table>

Additionally, I have included a search input box:

<input type="text" class="form-control mb20" id="search" placeholder="Search">

Attempt

In an attempt to enable search functionality for my table, I tried the following settings and scripts:

**include**

<script src="https://cdn.datatables.net/plug-ins/1.10.10/features/searchHighlight/dataTables.searchHighlight.min.js" type="text/javascript"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://bartaz.github.io/sandbox.js/jquery.highlight.js"></script>



**settings**

$('#account-table').DataTable({
    data: data,
    deferRender: true,
    paging: true,
    searching: true,
    bLengthChange : false,
    Filter: false,
    Info:false,
    searchHighlight:true,
    iDisplayLength: 10,
});


**trigger the draw while typing**

$('#search').keyup(function(){
  $('#account-table').search($(this).val()).draw();
});

Despite implementing these settings, the search functionality does not seem to be working as intended when typing in the search input box.

No errors are appearing in the console. How can I go about debugging this issue?

Answer №1

Don't bother creating your own search box, as DataTable will automatically generate one above the table for you.

Have you noticed a generated search box called:

<input type="search" class="" placeholder="" aria-controls="account-table">
? If not, check out this JSFiddle with your code and a functional filter and highlight feature: JSFiddle

If you do find yourself needing to use your own search box, here's the code to implement it:

 $('#search').keyup(function(){
   $('#account-table').DataTable().search($(this).val()).draw();
 });
 

The reason it may not have been working before is because using $('#account-table') retrieves jQuery results based on the selector, which are DOM elements that don't have a search function. Instead, by calling .DataTable(), DataTable returns the table object linked to that specific DOM table.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JSON sending error occurs if the data exceeds the maximum length limit when communicating with the Web

I'm encountering an issue while trying to send a JSON object to a .NET Web API. I have a multidimensional array that I convert into JSON and then send to the web service. The total length of the object exceeds 50,000 characters. However, if I slice th ...

Observing the closing of a modal window from a different controller in AngularJS

Within my main controller, I have a function called $scope.showDialog: $scope.showDialog = function(ev) { $mdDialog.show({ controller: 'DialogController', templateUrl: 'partials/dialog.tmpl.ejs', targetEvent: ev ...

Obtain a URL using JavaScript's regular expressions

Is it possible to use JavaScript regex to fetch the first function parameter? For instance, when I click on a tag in this page element, how can I extract the inline link? Here's an example: <li><a href="#blog" data-rel="clos ...

Tips for customizing the back button in Ionic 3

<ion-header> <ion-navbar> <ion-buttons left> <button ion-button navPop icon-only> <ion-icon ios="ios-arrow-back" md="nbsons-arrow-back"></ion-icon> </button> </ion-buttons> <ion-title> ...

Why is Firebug displaying an inaccessible JavaScript object property?

I am struggling to access the content of an object and can't seem to figure out why. It seems like maybe the properties that Firebug shows the object has are not actually there, or I might just be using the wrong syntax to access them. Here is the fu ...

Suboptimal placement of second submenu in Navbar

This question is inspired by a discussion on Stack Overflow that you can read here. Everything seems to be working fine, except when the 2nd level item is not at the top. In the example provided, the item with the 2nd level menu is the first item. If you m ...

Is it possible to request/scrape pages from the client side?

Let me present the issue at hand: I am currently managing a web application that serves as a notification system which updates frequently. This application is operational on several local computers, each of which solely display information without any inp ...

Determine the gravitational force of a planet on falling objects and display the result in an input field

I have developed a form code that includes several dropdown menus with different options, as well as an alert popup message to prevent users from selecting the same planet more than once. Additionally, I have disabled one input in a textbox because only on ...

What could be the reason for the jquery click event not working?

When viewing default.aspx, you can click on the + symbol to increase the quantity and the - symbol to decrease the quantity. <div class="sp-quantity"> <div class="sp-minus fff"> ...

I am having trouble retrieving the specific ID value using jQuery from the table row, as it is displaying as 'undefined'

I am attempting to capture the value from a table row when the add to cart button is clicked. The table structure can be found below. <table class="table" id="myTable"> <thead class="thead-dark"> <tr> <th scope= ...

Show submenu in Bootstrap 4 dropdown on the right-hand side

I am working with a Bootstrap 4 multilevel dropdown menu and I am trying to figure out how to display the submenu on the right side of the parent dropdown, rather than just aligning it to the right. Any suggestions on how to achieve this? <script src ...

Leverage multiple elements in an array in JavaScript while incorporating MQTT

I have a project using mqtt and I am trying to display data from different topics. Below is my script with one topic that works: <!DOCTYPE html> <html lang="fr"> <script src="https://unpkg.com/mqtt/dist/mqtt.min.js">& ...

Latest iOS and Safari updates are now stripping away classes that have been dynamically added through jQuery scripts during scrolling

Ever since the recent iOS Update (8+), Safari seems to be interfering with a jQuery script I rely on for my mobile navigations. The markup is a standard unordered list generated from Contao. What happens now is that when I view my page on iOS 8+, the scri ...

Struggling to pass a method header to event handling in React markup

Within my render() method, I have a component with event handling capabilities. render() { ... <input type="text" onChange={(_) => { this.renew("email", _.target.value); }} /> } private renew(type: string, input: any) { ... if (typ ...

When selecting an option in the burger menu, the dropdown does not react properly

I am facing an issue with the burger menu where one of the options should trigger a dropdown, but the content is not adjusting properly. The dropdown menu should push the other content downward, but currently, it overlaps. I have included the code for th ...

I utilized the "data-target" attribute to ensure the active link retains its style. Is there a way to preserve the style when navigating away from an active link?

Can someone assist me with this re-upload? I need help tweaking my code to maintain style when navigating between pages using the "data-target" attribute. Currently, the style is being lost when moving from the original link (e.g., "link/sub01.html") to a ...

Encountering ReferenceError while running production build with Webpack

After upgrading to webpack 3.10.0 and Babel 6.26, I managed to fix my dev build but encountered issues with the prod build that I can't seem to resolve. This is the error message I am seeing: ERROR in ./src/index.js Module build failed: ReferenceErr ...

Ways to retrieve the content and characteristics of a span element

I have this code snippet : <br> var value = $("#editor").jqxEditor('val'); value="<div><span style="font-weight:bold">Welcome</span></div>"; Now I want to extract the text inside the span tag and its font-weight a ...

using javascript to target a specific css selector attribute

I have a CSS file with the following code: .datagrid table tbody td { color: #00496B; border-left: 1px solid #E1EEF4; font-size: 16px ;font-weight: normal; } Is there a way to use JavaScript to dynamically change the font size? The code below worked ...

How to handle website addresses in Django

My current project involves creating a form with dropdown options that generate a URL based on the user's selection. Upon clicking the submit button, this URL is displayed as a clickable link on the page. When clicked, it redirects to the correspondin ...