Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect if a table exists (which doesn't have a class or ID)

Tags:

jquery

I’m having a problem trying to detect if a table exists using jQuery. The table has no class or ID.

What I’m trying to achieve is to not have the following code fire unless a table exists:

function tableAltRows()
    {
        $("#content table tr:even").each(function(){
            $(this).addClass("alt");
        });
    }
$(tableAltRows);

So I changed the last line to:

if ($('table').length > 0) {
    $(tableAltRows);
}

But the line checking the table length never returns anything other than 0. As a test, if I change it to == 0 it calls the tableAltRows function. I’m not that familiar with jQuery, so I assume I’m missing something obvious?

like image 744
tHeSmUrF Avatar asked Jul 22 '10 00:07

tHeSmUrF


People also ask

How do you check whether a table exists in database or not?

Using the OBJECT_ID and the IF ELSE statement to check whether a table exists or not. Alternative 2 : Using the INFORMATION_SCHEMA. TABLES and SQL EXISTS Operator to check whether a table exists or not.

How do you check if data not exists in a table SQL?

To test whether a row exists in a MySQL table or not, use exists condition. The exists condition can be used with subquery. It returns true when row exists in the table, otherwise false is returned. True is represented in the form of 1 and false is represented as 0.

How do you check if a table exists in Oracle?

declare cnt number; begin select count(*) into cnt from user_tables where table_name = 'EMP1'; if cnt>0 then execute immediate 'truncate table emp1'; DBMS_OUTPUT. PUT_LINE('Truncated the table'); else execute immediate 'create table emp1 as select * from emp'; DBMS_OUTPUT.

How do you know if a table exists or not in a snowflake?

if table exist or not. cal IF_table_exist('table_name') or select iftableexist('table_name');


2 Answers

I suspect that you're not calling your function when the DOM is ready. Try:

$(document).ready(function() {
    if($('table').length) {
        alert('hello');
    }
});
like image 94
karim79 Avatar answered Sep 19 '22 02:09

karim79


If you are calling the element before it exists, it will not work.

You can:

  1. Insert the script after the element
  2. Make the script execute when the page is ready

See this example

like image 21
BrunoLM Avatar answered Sep 22 '22 02:09

BrunoLM