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

Data Engineeer, Business Analytics, Web Developer, Library Technology specialising in PHP and Tableau

Based in Melbourne, Australia

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