Reputation: 12169
Is there anyway to implement the following using pure javascript?
if ( ! $('body.posts.show').length && ! $('body.posts.edit').length && ! $('body.chunks.create').length)
{
// Something here
}
Upvotes: 0
Views: 88
Reputation: 2154
You could use Javascript document.querySelectorAll('')
! try this
if (!document.querySelectorAll('body.posts.show').length && !document.querySelectorAll('body.posts.edit').length && !document.querySelectorAll('body.chunks.create').length)
{
// Something here
}
even better would be
if (!document.querySelectorAll('body.posts.show, body.posts.edit, body.chunks.create').length)
{
// Something here
}
Upvotes: 1
Reputation: 237845
You really want to get a single collection with all those selectors and see if it's empty.
In jQuery, you'd do this:
if ( ! $('body.posts.show, body.posts.edit, body.chunks.create').length )
The vanilla Javascript approach isn't much more difficult:
if (!document.querySelector('body.posts.show, body.posts.edit, body.chunks.create')) {
See the MDN documentation for document.querySelector
.
Upvotes: 3
Reputation: 56509
Yes!! I would suggest to go with querySelector
and querySelectorAll
methods.
var body = document.querySelector('body');
var posts = body.querySelector('posts');
var chunks = body.querySelector('chunks');
var show = posts.querySelectorAll('show');
var edits = posts.querySelectorAll('edit');
var create = chunks..querySelectorAll('create');
if ( !show.length && !edit.length && !create.length) {
// Something here
}
Upvotes: 1