Programming Tips - jQuery: check if something exists

Date: 2013sep20 Updated: 2022oct21 Library: jQuery Language: javaScript Q. jQuery: check if something exists A. Use .length like this:
if ($('#mything').length == 0) { // Does not exist } else { // Is there }
In fact you don't even need the compare, this is valid:
if ($('#mything').length) { // Is there }
Do NOT write length() We were just checking for one id but it works for any selector. For example to see if the document has any images:
if ($('img').length == 0) { alert('No images'); }