How do I know if there is a block with class "media" on the page?
if(<div class="media"></div> exist in current page){
//then do something
}
This code doesn't work:
if($(".media")){ //do }
How do I know if there is a block with class "media" on the page?
if(<div class="media"></div> exist in current page){
//then do something
}
This code doesn't work:
if($(".media")){ //do }
You need to check if ($('.media').length)
.
$(...)
returns a jQuery object, which will always be "truthy", even when empty.
However, if it's empty, its length
property will be 0
, which is "falsy".
You can also be more explicit and write if ($('.media').length > 0)
.
You can use .length to check for existance
if ($(".media").length)
{//do something}
One way to do it is:
if ($("div.media").length > 0) {
// Then do something.
}
Hint: use the tag name div
before the class .media
because it is more efficient.