How to check if a element exists in Javascript / jQuery

To check if a element exists in Javascript you can use the following snippet.

Sample Javascript

if (document.getElementById("elementid")) {
//element exists
}

Sample jQuery

if ($("#elementid").length) {
//element exists
}

Please Share us:
Please rate this snippet

2 thoughts on “How to check if a element exists in Javascript / jQuery

Leave a Reply