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
}
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
}
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')) {
querySelector will only the first occurrence of the element, so it has to be querySelectorAll in order to fetch multiple elements.body element in a page!But we're checking to find if there are no elements valid point :)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
}
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
}