¡¡¡UPDATE!!!
See my answer [ HERE ] for a MUCH more robust plugin!
You could easily shorten @Chris answer with:
if($("#container .search_element").length) { ...
You could also do the same with a useful plugin i made for this:
jsFiddle
(function($) {
if (!$.exist) {
$.extend({
exist: function(elm) {
if (typeof elm == null) return false;
if (typeof elm != "object") elm = $(elm);
return elm.length ? true : false;
}
});
$.fn.extend({
exist: function() {
return $.exist($(this));
}
});
}
})(jQuery);
USE
// With your if statement
if($("#container .search_element").exist()) { ...
// With ID
$.exist("#eleID");
// OR
$("#eleID").exist();
// With class name
$.exist(".class-name");
// OR
$(".class-name").exist();
// With just tag // prolly not best idea aS there will be other tags on site
$.exist("div");
// OR
$("div").exist();
Of course this plugin could be further extended to be much more fancy (handling multiple calls at once, creating non-existing elements based on a pram), but as it stand now, it does a very simple, very needed function ... Does this element exist? return True
or False
jsFiddle