andrew
andrew

Reputation: 488

jquery .toggle event activating

I have a jquery program at jsfiddle, and I need help because for me the jquery automatically calls the .slideUp(). Can you help me with this? I just want it so that when I click the hyperlink it hides the test text and when it is clicked again it shows it. Another problem is that the code is also using .slideUp() on the hyperlink. What do I do?

HTML:
<a href="#test">Test</a>
<div id="test">Test text</div>

jQuery:
$('a').toggle(function () {
var box = $(this).attr('href');
$(box).slideUp();
}, function () {
var box = $(this).attr('href');
$(box).slideUp();
}

Upvotes: 0

Views: 38

Answers (1)

Ram
Ram

Reputation: 144699

The toggle event method was deprecated in jQuery 1.8 and removed in jQuery 1.9, what you are using is the toggle effect method which toggles the visibility of the elements. You can use the slideToggle method instead:

$('a').on('click', function () {
    $(this.hash).slideToggle();
});

http://jsfiddle.net/9qjtct1x/

Upvotes: 1

Related Questions