How to check if a div exists with jQuery

The simple way to check if a div or element exists on the page using jQuery is to use the length function

jQuery check div exists

if ($('ul.author').length)
{    
    //do xyz
}

Using plain Javascript you need check for the element using its ID

Javascript check div exists

if(document.getElementById("element_id") !== null)
{
    //do xyz
}

Justin Kelly

Justin Kelly

Web Developer, Business Analytics, Data Engineer specialising in PHP, ReactJS, Tableau, AWS RedShift and Matillion ETL

Based in Melbourne, Australia

Feel free to contact me justin@kelly.org.au or _justin_kelly

Leave a comment

Name Notify me of replies by email.