There is no such thing as "JQuery functions", the JQuery code just usually uses an anonymous Javascript function.
To move the contents of the loop into a named function would look like this:
$(".x").click( function() {
for (var i=0; i<50; i++) toggleItem(i)
});
function toggleItem(i) {
if ($("#x"+i).is(':hidden')) {
$("#x"+i).show();
} else {
$("#x"+i).hide();
}
}
However, you could use the cascading properties of CSS to toggle all the items with a simple javascript statement instead of looping through all the elements. Example:
CSS:
<style type="text/css">
.StateOne .InitiallyHidden { display: none; }
.StateTwo .InitiallyVisible { display: none; }
</style>
HTML:
<div class="StateOne" id="StateContainer">
<div class="InitiallyVisible">Visible first</div>
<div class="InitiallyHidden">Visible second</div>
<div class="InitiallyVisible">Visible first</div>
<div class="InitiallyHidden">Visible second</div>
<div class="InitiallyVisible">Visible first</div>
<div class="InitiallyHidden">Visible second</div>
<div class="InitiallyVisible">Visible first</div>
<div class="InitiallyHidden">Visible second</div>
</div>
Javascript:
$('.x').click(function() {
var s = document.getElementById('StateContainer');
s.className = (s.className == 'StateOne' ? 'StateTwo' : 'StateOne');
});