This is a follow up to my question from yesterday (Jquery Create "array" of .click functions".)
Yesterday, I thought that I would simplify what I asked, hoping that I could extrapolate. I could not, and there is still something I can't figure out.
Therefore this new question, and PLEASE, I do admit that I am a newbie, and my question might seem soooo stupid for some of you, be patient with me!
I have a set of double divs:
<div id="div_a_1" class="pingpongdiv">hkh</div><div id="div_b_1" class="pingpongdiv">nuigyuig</div>
<div id="div_a_2" class="pingpongdiv">uynyn</div><div id="div_b_2" class="pingpongdiv">,jomoi</div>
<div id="div_a_3" class="pingpongdiv">yfvrcers</div><div id="div_b_3" class="pingpongdiv">rdcsers</div>
At present time, the above code is written by hand, and I plan on generating it with Jquery in the future, based on the number sets of divs I need.
They got the following css:
.pingpongdiv { /* make divs nearly invisible */
opacity: .15;
}
What I want to achieve with them is the following:
When I click on an "a" or a "b" div, its opacity is brought from .15 to 1 (becomes visible) by an animation. There is a catch, though: I must have clicked on the "a" div at least one time before I get to "activate" the corresponding "b" div!
I have the following jquery code (remember, I am new to this, and I might be using very cumbersome methods where it could be a lot more elegant, help me to it):
// define the array of flags used to check whether an "a" div was clicked on.
var leftPictureSeen = [];
leftPictureSeen[0] = 0; // never used because divs names start at 1
for (var i = 1; ; i++) {
if ($('#div_a_' + i.toString()).length) // If this div exists, add one more element to the array
leftPictureSeen[i] = 0;
else break;
}
// Attach the function that are needed for the divs to be seen and "unseen".
// ATTENTION: Here I mix what I learned yesterday about using "this"
// and I still have an "i" in the function definition, although I know that "i" is out of scope there.
// The idea is that I would like to know how I manage this issue properly so that it works
for (var i = 1; i < leftPictureSeen.length; i++)
{
$("#div_a_" + i.toString()).click(function(){
$(this).animate({opacity:1}, 1000);
// How do I replace the line below so that it works?
leftPictureSeen[i] = 1; // When an "a" div has been clicked, enable the "b" div click
});
$("#div_a_" + i.toString()).mouseout(function(){
$(this).animate({opacity:.15}, 1000);
});
$("#div_b_" + i.toString()).click(function(){
if (leftPictureSeen[i] === 1) // HOW DO I FIX THIS LINE SO THAT IT WORKS?
$(this).animate({opacity:1}, 1000);
else alert("You must click on the 'a' div first...");
});
$("#div_b_" + i.toString()).mouseout(function(){
$(this).animate({opacity:.15}, 1000);
});
};
It could be that there is a much simpler way to do all this, maybe by adding a css attribute to the "a" divs to indicate that they have been clicked on, what do I know!
And, if that was not enough, will somebody be kind enough to explain to me, or send me to a link somewhere that explains the issue of "i" being out of the scope of the .click function if that is what it is called?
Thank you very much for you patience ... and your help.