0

I don't understand why jquery script isn't working. Here is sample:

  $(document).ready(function(){
      $('#mygtukas-js').click(function(){
        $(this).data('clicked', true);
      });

      if($('#mygtukas-js').data('clicked')){
        $(this).next('#turinys').slideToggle(500);
      }
      else {
        $('#turinys').hide();
      }
  });

JSFIDDLE: http://jsfiddle.net/5NmK9/

Julius J
  • 103
  • 1
  • 2
  • 8

2 Answers2

1

Your slideToggle block is called once, on document.ready. When you click on #mygtukas-js, it changes 'clicked' to true, but it doesn't call the if/else block.

You should have the if/else block be within the code called after click, or have it within its own event listener.

Mike Manfrin
  • 2,722
  • 2
  • 26
  • 41
1

This should be enough:

$(document).ready(function(){
    $('#mygtukas-js').on("click", function() {
        // The line below can be removed if you're not using the data
        // value anywhere else.
        $(this).data('clicked', true);

        $(this).next('#turinys').slideToggle(500);
    });
});

The .slideToggle() will do the show/hide.

Demo: http://jsfiddle.net/5NmK9/1/

emerson.marini
  • 9,331
  • 2
  • 29
  • 46