In jQuery, I have written a piece of code that appears on every page of my website.
var d = $('#someElement').offset().top;
However, not every page on my website has an element with an ID of "someElement". Unfortunately, on these pages that lack such an element, no jQuery code works.
To solve this problem, I want to test if an element on the page indeed has the ID of "someElement" and then only run the code snippet above if there is.
How do I perform this test? Will this test solve the problem? Thank you.
$('#someElement').length
will return 1
if the element exists, 0
otherwise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With