how to find if specific element exists in html


I am trying to stick the ads on website while user scroll it, using the code below

//to Stick Ads while scroll on Desktop
    var StickHead=$('#StickHead').offset().top;
    $(window).scroll(function () {
        if ($(window).scrollTop() > stickyHeaderTop) {
            $('#StickHead').css({ position: 'fixed', top: '0px' });          
        } else {
            $('#StickHead').css({ position: 'static', top: '0px' });          
        }
    });

this code work's fine when we have div with id StickHead, but it throws an error when there is no StickHead id, as it would be difficult to copy paste it in selected page I have placed this code in script which is on all pages, now my question how can I make it work in all pages without error?
I have thought it would be easier if I can find if the Id exists, then run this code, otherwise leave it.
I have idea but not able to implement, any help?

thanks

 


Asked by:- vikas_jk
0
: 705 At:- 6/13/2017 7:29:10 AM
jquery javascript html









Login/Register to answer
Or
Post answer directly by entering your email-id & Name

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use