There are some issues with the code, some pointed out by the comments, but the main one is how you are approaching the onClick event. You are wiring up your click event inside a function then calling it, you should have defined it within the $(document).ready
scope instead, also you would not need an inline onClick event after you do that.
Here is a JSFiddle with my 'recommended' code. I hope it helps you out.
HTML:
<button class='toggle-button switch-on'>Test</button>
<div id="color-list">Container Contents</div>
JAVASCRIPT:
$(document).ready(function(){
$('.toggle-button').click(function(event){
if($(this).hasClass('switch-off')){
$(this).removeClass('switch-off');
$(this).addClass('switch-on');
$('#color-list').hide();
} else {
$(this).removeClass('switch-on');
$(this).addClass('switch-off');
$('#color-list').show();
}
event.preventDefault();
});
});
If you still want to keep a separate function where you can still send the ElementId as a variable, you can do something like that: (http://jsfiddle.net/V54fJ/3/)
$(document).ready(function(){
$('.toggle-button').on('click',
function(event){
toggleVisibility(this,'color-list');
event.preventDefault();
});
function toggleVisibility(caller,elementId)
{
if($(caller).hasClass('switch-off')){
$(caller).removeClass('switch-off');
$(caller).addClass('switch-on');
$('#'+elementId).hide();
}
else{
$(caller).removeClass('switch-on');
$(caller).addClass('switch-off');
$('#'+elementId).show();
}
} });