In this programming tutorial you will learn how to check whether an element exists using jQuery. Fortunately it is very easy in jquery. Let’s have a look over example given below.
How to check whether an element exists using jQuery
If an element exists then the length of that element will be 1 else it will be 0.
So that’s it.
I love your feedback. Stay tuned :)
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
<!--
if ($("#mydiv").length > 0){
// div exists
// do something here that you want to do
}
// -->
</script>
With a jQuery selector we can also use the length property, which will return the size of the object. If we don't use length property while using a selector, jQuery will always return an object. So the if statement will always be true and never be false.If an element exists then the length of that element will be 1 else it will be 0.
So that’s it.
I love your feedback. Stay tuned :)

0 comments:
Post a Comment