How to check if value exist in datatable
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Data Retrieval: How to Find and Extract a Single Row from a DataTables Instance
As developers working with dynamic data tables, one of the most common challenges is moving beyond simply displaying the table to actively searching within it and extracting specific records based on user input. You are right; knowing how to iterate through or select a single row based on a condition is crucial for building interactive applications.
This post will walk you through the best practices for checking if a value exists in a DataTables instance, finding that corresponding row, and storing its entire data set into a JavaScript variable. We will move beyond simple selection methods to implement robust search logic.
Understanding the DataTables API
DataTables provides a powerful JavaScript API that allows you to control the table's behavior, including searching, sorting, and retrieving the underlying data. To achieve complex querying, we must leverage this API rather than relying solely on raw DOM manipulation.
The method you mentioned, $('#datatable').DataTable().rows({selected:true}).data();, is excellent for selecting rows that have already been identified (perhaps via custom logic or filtering). However, when you need to search dynamically based on user input against the table's content, we need to interact with how DataTables handles its internal data structure.
Method 1: Searching and Extracting Data Programmatically
Since DataTables is designed for filtering, the easiest approach is often to utilize its built-in search capabilities first. If you want to find a specific row based on an external input (e.g., searching for "two" in a column), you need to iterate over the data provided by the API.
Here is a step-by-step guide on how to implement the requested logic: checking for existence and storing the full row data.
Step 1: Initialize the Search Logic
Assume your DataTables instance is initialized and accessible via the selector #datatable. We will use the .rows.data() method, which returns an array of all rows in the table, where each row is an array of cell values for that record.
function findRowByValue(inputValue, columnIndex) {
// Get the DataTables instance
const dataTable = $('#datatable').DataTable();
// Get all the data rows (excluding the header)
const allRows = dataTable.rows().data();
for (let i = 0; i < allRows.length; i++) {
// Check if the value in the specified column matches the input
if (allRows[i][columnIndex] === inputValue) {
// Value found! Store the entire row data.
const rowData = allRows[i];
console.log("Row found:", rowData);
return rowData; // Return the data and exit the loop
}
}
// If the loop finishes without finding a match
console.log(`Value "${inputValue}" not found in column ${columnIndex}.`);
return null;
}
// Example usage: Search for the value 'two' in the 2nd column (index 1)
const searchedData = findRowByValue('two', 1);
if (searchedData) {
var data = searchedData; // Storing the whole row into the desired variable
console.log("Successfully retrieved data:", data);
} else {
console.error("Error: Row could not be located.");
}
Step 2: Handling Data Structure and Performance
The approach above is highly efficient because it iterates only over the data array returned by the DataTable, avoiding unnecessary DOM searches. This mirrors how structured data handling works in frameworks like Laravel, where you fetch a specific Eloquent model based on a query—you are looking up a specific record within a larger collection. When building robust applications, understanding this relationship between front-end presentation and back-end data structures is key, much like ensuring your models adhere to clean definitions found on platforms like laravelcompany.com.
Conclusion: Building Robust Data Interactions
Finding a specific row in a dynamic table requires shifting focus from simply selecting elements to intelligently querying the underlying data structure provided by the library. By utilizing the DataTable API's .rows().data() method and implementing a focused loop, you gain complete control over your data retrieval process. This technique allows you to check for existence with an if/else condition before extracting the full record, providing clean error handling and a superior user experience compared to blind searching. Mastering these interaction patterns is fundamental to developing sophisticated front-end applications.