as all you know
$("#ID")
returns the element having ID.
but this code always return even there’s no element.
alert($("#htrBuyerCouponNotice"));
alert(document.getElementById("htrBuyerConponNotice"));
in this case.
those two line results are diffrent.
I want to check whether there is an element has htrBuyerCouponNotice.
document.getElementByID return null if there’s no element.
You can check the
lengthproperty of the jQuery object to determine the number of matched elements, e.g.:You can use it directly on
ifstatements e.g.:But most of the time you don’t really need to know if the selector matched elements or not, because the jQuery built-in methods will be simply ignored, e.g.:
The above statement will not cause any error even if the element was not found.
jQuery has also the
sizemethod, but I would recommend you to use thelengthproperty directly since it’s publicly accessible, thesizemethod is slightly slower since it is only a function that returns the value oflengthproperty.