1

I need some help with my website. I want to integrate the jquery toggle function. But it doesn't work. I don't have a lot experience with jQuery.

Here is my code:

<a href="javascript:toggle('exp');">Klick</a>

<div id="exp" style="display:none;"> hallo </div>

<script src="jquery-ui-1.10.3/js/jquery-1.9.1.js"></script>
<script src="jquery-ui-1.10.3/js/jquery-ui-1.10.3.custom.js"></script>
<script>
function toggle(str)
{
    $(str).slideToggle('slow');
}   
</script>

Has anyone an idea why it doesn't work?

mkj
  • 2,761
  • 5
  • 24
  • 28
nicoschuck
  • 201
  • 3
  • 13
  • Here is a good learning resource for selectors: http://www.w3schools.com/jquery/jquery_selectors.asp – Scott Fanetti Sep 13 '16 at 13:53
  • You should using a proper event-handler instead of having a js-function in the `href`, see [here](http://stackoverflow.com/a/11348403/4202224) for a discussion about this topic – empiric Sep 13 '16 at 13:56
  • i know this option and i already use it. But i didnt know why its better. Thank u it is very interesting – nicoschuck Sep 13 '16 at 14:17

2 Answers2

2

Missing # before id selector in your code.

function toggle(str){
    $('#' + str).slideToggle('slow');
    //-^----
}   
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

You need to specify what to toggle.

$("#exp").slideToggle('slow');

See Jquery doc: http://api.jquery.com/toggle/

Jens Hunt
  • 36
  • 4