In jQuery, you can use the .length property to check if an element is exists. if the element is exists, the length property will return the total number of the matched elements.

For example,

if($('#div1').length){
	alert("Div1 is exists");
}else{
	alert("Div1 is not exists");
}

To check if an element which has an id of “div1″ exists.

jQuery length example

<html>
<head>
 
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
 
</head>
 
<body>
 
<h1>jQuery check if an element is exists</h1>
 
<script type="text/javascript">
 
  $(document).ready(function(){
 
    $("#buttonDiv1").click(function () {
 
	if($('#div1').length){
		alert("Div1 is exists");
	}else{
		alert("Div1 is not exists");
	}
 
    });
 
    $("#buttonDiv2").click(function () {
 
	if($('#div2').length){
		alert("Div2 is exists");
	}else{
		alert("Div2 is not exists");
	}
 
    });
 
  });
</script>
</head><body>
 
<div id="div1">
	<b>This is DIV element which has an ide of "div1"</b>
</div>
 
<br/>
<br/>
<br/>
 
<input type='button' value='div1 is exists?' id='buttonDiv1'>
<input type='button' value='div2 is exists?' id='buttonDiv2'>
 
</body>
</html>
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at jQuery Tutorials