Jquery is the instance code that detects the existence of an element


if ($(‘#myDiv).length) {      // your code  }

 Simple, but not obvious.