The problem is that you are calling the toggleTable function when you attach it. That is why it works when creating an element.
span_ele.addEventListener('click', toggletable(column_list[i]),true);
To avoid this, you need to:
span_ele.addEventListener('click', toggletable, true);
But obviously this does not go in the column for switching, so it is not perfect.
I would use something like:
function create_weekmenu(json) { var column_list=json.week_list; var menu_table=document.getElementById("weekmenu"); var row=document.createElement('tr'); for(var i=0;i<column_list.length;i++) { var cell=document.createElement('th'); var span_ele=document.createElement('span'); if(span_ele.addEventListener) { span_ele.addEventListener('click', function(col) { return function() { toggletable(col); } }(column_list[i]),true); } else if(span_ele.attachEvent) {
You need to make sure that you attach the function to the event handler and not to the result of the function.
source share