Im trying to call if div exists with the following code.
Div 1 and 2 are recognized when tested, but it doesnt recognize div 3.
Any idea what i’m doing wrong?
<script type="text/javascript">
if($('.3').length){
alert("Div exists");
}else{
alert("Div not exists");
}
</script>
<div id="1">
<div class="2">
<div class="3">test</div>
</div>
</div>
I don’t think the code will work for .2/#1 either, since you are trying to access the elements even before they are available in DOM.
Wrap the JS code in jQuery ready event handler.
Try this: