jquery - how to do detect if a div is still in the DOM -
i want know if div exists within dom or not. have notification on site, @ top of page, user can click on it, , slideup. have jquery event depends on knowing whether div still exists in dom. want simple script produce boolean value of whether of not div exists or not. tried
if($('#cookies_notification').css('display','block'))
which seems acknowledge if div there, need know if not there, tried using 'else' no luck.
so what's best way check if element exists in dom or not?
update ok, mistaken, don't think leaves dom on slideup, need know if sliedup on element has occured.
update 2 guess add $('#cookies_notification').css('display','none')
click event when notification scrolls up, detect it. that's solution can conceive.
update 3 oh, adding css code in removes slideup effect.
$("#cookies_notification").click(function(){ $(this).slideup().css('display','none');
});
how can make slideup , change display?
if selector returns 0 elements not located in dom.
if( $('#cookies_notification').length === 0 ) { // }
here working example: http://jsfiddle.net/lt7g8/
alternatively, see question: is there “exists” function jquery?
if wondering whether object hidden (and not absent dom), check out jquery :hidden
selector. if element exists has been hidden, selector return element. not select element if not exist, nor if exist , visible.
for situation sounds want detect first if element exists, , detect whether has been hidden slideup()
.
var objcookienotification = $('#cookies_notification'); if ( objcookienotification.length === 0 ) { // notification not exist in dom. // add dom here. } else if ( objcookienotification.is(':hidden') ) { // notification exists in dom hidden. // make visible again here. } else { // notification exists in dom , visible. // if need make flash or something, can here. }
Comments
Post a Comment