Trouble Ensues When Using Two Jquery Datatables with the Same Class

I am facing an issue with two jQuery datatables that have the same class names. The search functionality works in one table but not in the other. I need some help reviewing my code to figure out how to fix this.

jQuery

$('.dataTable th.searchClass').each( function () {
        var title= $(this).text();
        $(this).append( '<br/><input type="text" class="searchClassIcon" placeholder="Search '+title+'" />' );
    });

/*applying dataTable*/
     var table=$('.tab-section table.dataTable').DataTable({
            "bJQueryUI": true,
             "bSort": false,
            "sPaginationType": "full_numbers",
            "bPaginate":false,
            "bFilter":true,
            "sType":"string"

        });

// Apply the search
        table.columns().eq(0).each( function ( colIdx ) {
        $( 'input', table.column( colIdx ).header() ).on( 'keyup change', function () {
            //console.log("search");
            table.column( colIdx ).search( this.value ).draw();
        } );

Answer №1

When applying the dataTable function to multiple elements and assigning them to the variable "table," it's important to specify which table you're referencing in your search function. Otherwise, it will default to the first one.

An effective approach is to apply the event without using the table variable initially. Then, within the event, you can use

$(this).parents('table').dataTable()
to target the specific table that is being accessed.

Answer №2

To distinguish between two data tables, the key is to utilize unique id attributes instead of classes for each table. This allows you to easily access and manipulate the search functionality corresponding to each 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

Enabling jQuery droppable functionality when double-clicked

Looking at the HTML rendered on my MVC5 page, I've successfully implemented dragging elements from #cardPiles and dropping them into #cardDrop divs. Each #cardDrop div can only accept one element, hence why the droppable = disabled option is used on d ...

Utilizing the Jackson Framework in Spring Boot for effective mapping with @RequestBody annotation

Hello everyone. I am dealing with some data fetched from a jQuery DataTable and sent to a Spring Controller using Ajax. The ajax function snippet looks like this: $.ajax({ url: "../todaydatarecover.json", t ...

Efficiently loading Google Visualizations for core charts by utilizing AJAX within jQueryUI tabs

Loading Google Visualizations via AJAX in jQueryUI tabs After encountering a similar issue, I discovered a solution provided in the link above that partially resolved my problem. I am trying to use Google Visualization core charts within jQueryUI tabs wit ...

Stop automatic scrolling after one full rotation

I've implemented a cool panorama script from this link: http://jsfiddle.net/zFft4/24/ I've managed to make the animation stop at the end of the first loop. However, I'm facing a challenge in getting the animation to halt at the END OF THE I ...

Conditions in Apache mod_rewrite

Having trouble with Apache's module rewrite (creating browser-friendly URLs). My goal is to redirect every request to a specific PHP document if it contains a certain string: RewriteBase /folder/directory/ RewriteCond %{REQUEST_FILENAME} !-f RewriteC ...

Execute a jQuery function upon the selection of a browser's auto-suggested option

After entering an email address into the input field, my code will change the border color to green if it is a valid email and red if it is invalid. html: <input class="ui_input2 no_space" id="input_email" type="email" name="email" style="height:25px ...

Hierarchy-based dynamic breadcrumbs incorporating different sections of the webpage

Currently in the process of developing a dynamic breadcrumb plugin with either jQuery or Javascript, however I am struggling to implement functionality that allows it to change dynamically as the page is scrolled. The goal is to have a fixed header elemen ...

Is it feasible to make Twitter's Typeahead plugin have an initial slide down or fade in effect?

Does anyone have an easy way to make the dropdown from Twitter's typeahead jQuery plugin initially slide down or fade in? I'm trying to animate the size of the dropdown menu when it adjusts, but even a simple fade or slide in would be fine. I pre ...

Having trouble triggering the onclick event on a dynamically created table using JavaScript

I am facing an issue with adding a table programmatically that has an onclick event triggering a specific JavaScript function using the row's id for editing purposes. Unfortunately, the function is not being called as expected. I have attempted to mod ...

What steps can be taken to execute a function when a button remains unclicked?

$("#RunCode").click(function(){ var $this = $(this); if($this.data('clicked')) { console.log("Is clicked"); $(".documentWrite").text("Is clicked"); } else { $this.data('clicked', true); consol ...

When checking for errors with console.log(errors) in Ajax, it may return undefined due to server-side

I'm looking for a way to alert the user about validation errors coming from PHP. I am currently using Laravel 5.3 to build the form and Ajax to retrieve error messages. Below is my JavaScript code: $.ajax({ url: '/artistPost', typ ...

Verify the accuracy of quiz responses with PHP and AJAX

I am working on a picture quiz that has one input field for each image. I am looking for a solution to verify if the value entered into the input field matches the correct answer. If it does match, I need to perform certain operations like adding or removi ...

Unable to choose the div element with id ":1"

Just starting out with web development, and I have a div element with the following attributes: <div class="" id=":1" role="treeitem" aria-selected="false" aria-expanded="false" aria-level="1" aria-labelledby=":1.label" aria-setsize="10" aria-posinset= ...

Is there a way to display the output of jQuery in an input box rather than a span?

I need to display the result of this function in an input box instead of a span tag because I plan to utilize the result in the following form. function checkChange(){ $.ajax({ url: "ordercalculate.php", data: $('form').seria ...

Is it possible to trigger the JavaScript mouseover function following a click event?

Is it possible to call a function on mouse over after the first click event is triggered by the user? <a href="javascript:void(0);" id="digit<?php echo $k;?>" onClick="javascript:return swapClass('<?php echo strtoupper($v);?>',&ap ...

Adding toggle switches dynamically to a JQuery mobile listview is a convenient and efficient way to

I am attempting to dynamically add a new Listview Item with a Toggle switch inside, triggered by a button click. However, I am only able to get a Checkbox inside the item. Can someone please guide me on what steps I should take? Here is my code: <!DOC ...

JavaScript - Modify input character prior to appending it to the text area

I am working on creating a virtual keyboard using jQuery. Whenever I press 'a' in the textarea, I want it to display 'z' instead. In my investigation of typing a letter in the textarea, I discovered the following sequence: keyDown ev ...

Problematic PHP/AJAX: How come my getAttribute function is only selecting the initial line within the foreach loop?

My goal here is to display a list of users, each with a button that sends unique IDs to the database. The issue I'm facing is that the code only works for the first user in the list and not the rest. I suspect this is because every list item has the s ...

Choose button based on its value

What is the best way to target a button with a specific value using jQuery? <input type="button" value="My task"> ...

What is the purpose of using *//<![CDATA[* and *//]]>* in a jQuery code snippet?

Check out this JavaScript code snippet that I have: <script type="text/javascript> //<![CDATA[ jQuery(document).ready(function() { jQuery("#page_template option[value='sidebar-page.php']").remove(); }); ...