I am new to JQuery/Javascript. Is there a good way to put bunch of elements in an array and loop through it, call a function to do something. Lets me explain what I am trying to do.
I have this code below will hide and show the 2 DIVs when the mouse click outside of the DIV. It works great. However, I have alot of DIVs to show and hide. Please show me a way to put in a array of DIVs, convert below code to the function ShowHideDIV(hideDiv, showDiv) to perform the action?
$(document).mouseup(function (e)
{
var showContainer1 = $("#divShipMethod");
var hideContainer1 = $("#divShipMethodDDL");
if (!hideContainer1.is(e.target)
&& hideContainer1.has(e.target).length === 0)
{
hideContainer1.hide();
showContainer1.show();
}
});
For example, Says I have the following DIV IDs like to put in an array:
- divShow1
- divHide1
- divShow2
- divHide2
- divShow3
- divHide3
- divShow4
- divHide4
- divShow5
- divHide5
$.each()to loop through an array with jQuery.class="foo"to the divs) and then you refer to them all with$('.foo'). Then you could loop them like$('.foo').each(function () { /* do something */ };or perhaps hide them all with$('.foo').hide();with no loop at all.