javascript - loading a markup if a class exists -


i want load markup documents based on present of classes in document.but problem if definite class not exists @ loads.

my codes are

(function($){    'use strict';     var prefixmarkup = '<ul class="filter-elements hidden" >'+                           '<li><a href="#" class="current"></a></li>'+                         '</ul>';      if($('body').has('.filter-container')){       if(!$('body').hasclass('filter-elements')){         $('footer.footer').before(prefixmarkup)       }     } })(jquery); 

here if .filter-container not exists prefixmarkup loads , if .filter-elements exists prefixmarkup loads should now?

first:

  • the .hasclass() method return true if class assigned element, if other classes are.
  • the .has() method constructs new jquery object subset of matching elements.

your need change:

if($('body').has('.filter-container')){ 

to:

if($('body').has('.filter-container').length){ 

fiddle (example): https://jsfiddle.net/lmgonzalves/c8nwp813/1/

learn more here , here.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -