hellzone
hellzone

Reputation: 5236

how to use .toggle with jquery 2.1

Function toggle() is deprecated and I don't know how to handle this problem. I tried some solutions but none of them worked for me. Here is my last try(still doesn't working);

<script>
$("#clickId").click(function(){
 var $block = $("#myDiv"),
     $this = $(this);
 $block.slideToggle(function(){
     $this.text($block.is(':display') ? 'hidden' : 'block');
 });
});
</script>


<a href="#" id="clickId" ><img src="/img/sample.png" alt="sample"/></a>
    <div id="myDiv" style="display: none !important">

EDIT: I changed below lines but still not working.

$this.text($block.is(':visible') ? 'false' : 'true');
<div id="myDiv" visible="false">

Upvotes: 2

Views: 448

Answers (3)

nodoubt
nodoubt

Reputation: 21

The javascript didn't identify the $("#clickId") and $("#myDiv") because the dom didn't load. Use $(document).ready() or put your script after the dom (tag a and div)

Upvotes: 2

Curtis Mattoon
Curtis Mattoon

Reputation: 4742

The first parameter to slideToggle is the duration, then the callback function:

http://jsfiddle.net/jgzucro8/

$('#btn').click(function() {
    $('#mydiv').slideToggle(0.5, function () {
        $('#btn').text($(this).is(':visible') ? 'visible' : 'hidden'); 
    });
});

Upvotes: 1

Taylor Kolasinski
Taylor Kolasinski

Reputation: 31

I would just simplify your function a bit:

$("#clickId").click(function(){
   $("#myDiv").slideToggle();
});

http://jsfiddle.net/ssmmnfdd/1/

Upvotes: 0

Related Questions