I know you can check for width() or height() , but what if the element's display property is set to none? What else needs to be checked to make sure the item exists?
width()
height()
You can use length to find out if your selector matches anything.
if ($('#MyId').length) { // do your stuff }
Assuming you are trying to find if a div exists
$('div').length ? alert('div found') : alert('Div not found')
You can use the visible selector:
http://api.jquery.com/visible-selector/
jQuery should be able to find even hidden elements. It also has :visible and :hidden selectors to search for both visible and hidden elements.
:visible
:hidden
Does it help? Not sure without additional information.
if ($("#MyId").length) { ... write some code here ...}
This automatically checks for the presence of the element and returns true if the element exists.
I use this:
if ($('.div1').size() || $('.div2').size()) { console.log('ok'); }
Basically, I prefer to use this syntax:
if ($('#MyId')!= null) { // dostuff }
Even if this code is not commented out, the functionality is obvious.