1

I have seen lot of questions like this but none of them helped me I want to see when my span tag get available in DOM, my code looks like this

   <span id="inviteUpdate">0</span>

    if(jQuery("#inviteUpdate").html()>0){
        alert("Element exists");
    }

Am I missing something here?

Badddy
  • 59
  • 10

3 Answers3

2

If you want to test whether an element exists in the DOM, use .length:

if ($("#inviteUpdate").length > 0) {
    alert ("Element exists");
}

This is because jQuery collections can be used as arrays, returning the original DOM elements that were matched.

Barmar
  • 741,623
  • 53
  • 500
  • 612
2

You can use getElementById method. If the value is not null, it exists.

var span1 = document.getElementById('inviteUpdate');
if(span1 != null)
{
    alert('span exists');
}

works well.

mrsrizan
  • 301
  • 1
  • 8
1

  if(jQuery("#inviteUpdate").length >0){
        alert("Element exists");
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<span id="inviteUpdate">0</span>

check this

Man Programmer
  • 5,300
  • 2
  • 21
  • 21
  • The clause `jQuery("#inviteUpdate")` cannot be falsy. The first part of the condition serves no purpose. – Kijewski Sep 21 '14 at 07:40