How to Find the Correct TablePress ID for Your JavaScript or CSS
Content
If you've ever tried to use JavaScript's getElementById() or CSS to style a specific TablePress table, you might have run into a confusing issue: the HTML ID of your table seems to change unexpectedly. This can break your custom scripts and styles. This guide explains why this happens and how to reliably target your tables every time.
Why Does My Table's HTML ID Change?
TablePress generates an HTML ID for each table to make it unique on the page. Normally, this ID follows the pattern tablepress-{ID} (e.g., tablepress-4).
However, if the function that renders the table is called more than once on the same page load, TablePress will automatically append a suffix like -no-2 to prevent duplicate IDs in the HTML. This results in an ID like tablepress-4-no-2. This is a standard and necessary practice to ensure valid HTML, where element IDs must be unique.
This behavior can be triggered by certain themes, other plugins, or specific caching mechanisms that cause the table to be processed multiple times.
The Solution: How to Find the Correct, Stable ID
You should not hardcode an ID that includes the -no-2 suffix, as it is not guaranteed to be stable. Instead, you must inspect your live page to see what ID the table actually has when it is rendered.
- View the Page Source: Navigate to the page containing your table in a web browser.
- Open the HTML Source Code: Right-click on the page and select "View Page Source" (the exact wording may differ by browser).
- Search for Your Table: Press
Ctrl+F(orCmd+Fon Mac) to open the search box and search for<table. This will highlight all the HTML<table>elements in the source. - Identify the Correct ID: Look for the table with a
classattribute that containstablepress-id-{ID}. Theidattribute of this same<table>tag is the exact, correct ID you must use in yourgetElementById()function or CSS selector. This is the only ID guaranteed to be correct for that specific page load.
Example:
If you find <table id="tablepress-4-no-2" class="tablepress tablepress-id-4">, then your JavaScript should use:
document.getElementById("tablepress-4-no-2");
Best Practice for Reliable Code
While inspecting the source gives you the correct ID for that moment, a more robust long-term solution is to use a different method to target your table that doesn't rely on the full, potentially changing ID.
You can use the stable class attribute that TablePress assigns, which always contains tablepress-id-{ID} without the -no-2 suffix. You can use this class in a CSS selector or a more complex JavaScript query.
CSS Example:
To style the table with ID 4, you could use:
.tablepress-id-4 { /* your styles here */ }
JavaScript Example (using jQuery):
$('.tablepress-id-4').doSomething();
JavaScript Example (vanilla JS):
document.querySelector('.tablepress-id-4');
By using the class-based selector, your code becomes more resilient to changes in the full HTML ID, ensuring your scripts and styles continue to work correctly.
Related Support Threads Support
-
Stop creating new ‘posts’ in database when updating tableshttps://wordpress.org/support/topic/stop-creating-new-posts-in-database-when-updating-tables/
-
Get table as json or PHP structurehttps://wordpress.org/support/topic/get-table-as-json-or-php-structure/
-
Get table ID with post IDhttps://wordpress.org/support/topic/get-table-id-with-post-id/
-
Procedurally Replace Table H2 Generated IDshttps://wordpress.org/support/topic/procedurally-replace-table-h2-generated-ids/
-
Shortcodes and editing tables easilyhttps://wordpress.org/support/topic/shortcodes-and-editing-tables-easily/
-
getElementById(table_id)https://wordpress.org/support/topic/getelementbyidtable_id/
-
Add Custom Taxonomy to “tablepress_table” Custom Post Typehttps://wordpress.org/support/topic/add-custom-taxonomy-to-tablepress_table-custom-post-type/
-
Find out what table is being used via ‘View Source’ or ‘inspect’https://wordpress.org/support/topic/find-out-what-table-is-being-used-via-view-source-or-inspect/
-
How are table id’s stored in the database?https://wordpress.org/support/topic/how-are-table-ids-stored-in-the-database/
-
How get data from specific cell tablepresshttps://wordpress.org/support/topic/how-get-data-from-specific-cell-tablepress/
-
How to get the Post ID of a Tablehttps://wordpress.org/support/topic/how-to-get-the-post-id-of-a-table/
-
How to create table ids in version 3.0https://wordpress.org/support/topic/how-to-create-table-ids-in-version-3-0/
-
Get dynamic value in table presshttps://wordpress.org/support/topic/get-dynamic-value-in-table-press/
-
How to get the results of the tables in WP?https://wordpress.org/support/topic/how-to-get-the-results-of-the-tables-in-wp/
-
Create a table programmatically (PHP function)https://wordpress.org/support/topic/create-a-table-programmatically-php-function/
-
Finding where a table has been used?https://wordpress.org/support/topic/finding-where-a-table-has-been-used/