Alex Murray
Alex Murray

Reputation: 275

Javascript simple toggle not working

I know this has been asked before in various places but i cannot understand why this is working. On toggle it should show/hide the div. Can some tell me why? Thanks

JS:

jQuery('.box-toggle.fl.active').on('click', function(event) {   
    jQuery('#box-content').toggle('show');
  });

fiddle: https://jsfiddle.net/amurray4/cwdt4xeL/

Upvotes: 0

Views: 108

Answers (1)

Roko C. Buljan
Roko C. Buljan

Reputation: 206669

You need to include the jQuery library in your project

<script src="https://code.jquery.com/jquery-2.1.4.js"></script>

jsFiddle demo

also, since you use an anchor <a> to register the click, in order to prevent page-jump-to-top following an anchor use Event.preventDefault(), and to secure your jQuery $ alias use:

jQuery(function( $ ){ // Secured $ alias and DOM ready

  // Now use $ (for jQuery) freely:
  $('.box-toggle.fl.active').on('click', function(event) { 
    event.preventDefault();  
    $('#box-content').toggle('slow'); // Use "slow"
  });
  // other DO Mready code here

});

Upvotes: 1

Related Questions