snoopy_15
snoopy_15

Reputation: 1323

lightSlider is not a function, wordpress

I'm trying to add lightslider(http://sachinchoolur.github.io/lightslider/index.html) in WordPress but I got error

Uncaught TypeError: jQuery(...).lightSlider is not a function(anonymous function) @ (index):99m.Callbacks.j @ jquery.js?ver=1.11.2:2m.Callbacks.k.fireWith @ jquery.js?ver=1.11.2:2m.extend.ready @ jquery.js?ver=1.11.2:2J @ jquery.js?ver=1.11.2:2

I'm trying with the simplest example from their website

HTML

<ul id="lightSlider">
  <li>
      <h3>First Slide</h3>
      <p>Lorem ipsum Cupidatat quis pariatur anim.</p>
  </li>
  <li>
      <h3>Second Slide</h3>
      <p>Lorem ipsum Excepteur amet adipisicing fugiat velit nisi.</p>
  </li>
  <li>
      <h3>Third Slide</h3>
      <p>Lorem ipsum Excepteur amet adipisicing fugiat velit nisi.</p>
  </li>

</ul>

JavaScript

<script type="text/javascript">
//Also tried to add noConflict() function
//jQuery.noConflict();
jQuery(document).ready(function() {
jQuery("#lightSlider").lightSlider(); 

});
</script>

Can somebody help me? Thank you!

Upvotes: 5

Views: 5814

Answers (4)

Mario Gonzales Flores
Mario Gonzales Flores

Reputation: 705

Add the deferred load

<script src="js/lightslider.js defer"></script>

Upvotes: 0

WaNgeL
WaNgeL

Reputation: 198

You should put dependency array('jquery') to load jQuery first.

Example:

  wp_register_script( 'lightslider-js', get_stylesheet_directory_uri() . '/js/vendor/lightSlider/js/lightslider.min.js', array('jquery'), NULL, true );

Upvotes: 1

Cristian Michel
Cristian Michel

Reputation: 326

I had the same problem but I have fixed it. Just copy the entire code of lightslider.js and past it into:

$(document).ready(function(){
   //code of lightslider 

});

Upvotes: 1

snoopy_15
snoopy_15

Reputation: 1323

I resolved problem. The problem exist because somehow jQuery were loaded after lightSlider.js . I don't know how this happened, I just put jQuery to load first in wp_enqueue_script(), before anything else (before css, too) and lightslider.js at the end.

I hope this answer will help someone more. Thank you for help.

Upvotes: 2

Related Questions