Date: 2013sep20
Update: 2022oct21
Library: jQuery
Language: javaScript
Q. jQuery: Check if something exists
A. Use the .length property 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');
}