jQuery.fn.extend({ toggleVisibilityState: function() { var ATT_TOGGLE_VALUES = [ // attribute, closed state , open state ['value', '+', '-'], ['title', 'open solution (Did you try hard enough yourself?)', 'Close solution'] ]; var nextIndex; switch(this.attr(ATT_TOGGLE_VALUES[0][0])) { // First attribute defines reference case ATT_TOGGLE_VALUES[0][1]: nextIndex = 2; break; default: nextIndex = 1; break; } for (var r in ATT_TOGGLE_VALUES) { this.attr(ATT_TOGGLE_VALUES[r][0], ATT_TOGGLE_VALUES[r][nextIndex]); } } }); function initBbPost(index, bbForumElement) { var postingsUrl = "https://bb.mi.hdm-stuttgart.de/api/topic/" + bbForumElement.getAttribute("data-tid"); $.ajax({ type: 'GET', url: postingsUrl, dataType: "json", success: function (data) { var postcount = data.postcount - 1; var a = bbForumElement.getElementsByTagName("a")[0]; var text, style; if (0 == postcount) { text = "Create first comment in forum!"; style = "color: red"; } else { style = "color: green"; if (1 == postcount){ text = "Read or add comment in forum"; } else { text = "Read " + postcount + " comments or add one"; } } a.innerHTML = "<img src='common/images/comment.svg' width='40ex' style='vertical-align: middle;' alt=' " + text + "' title = '" + text + "'>" + text; a.setAttribute("style", style); } }); } $(document).ready( function () { $(".bbForum").each(initBbPost); $(".hideSolution").next().toggle(0); // Show or hide first following sibling $(".hideSolution").toggleVisibilityState(); // Initialization $(".hideSolution").click( function () { $(this).toggleVisibilityState(); $(this).next().slideToggle(); // Show or hide first following sibling } ); } );