0

Failing to understand why the code isn't working ...

the code worked fine before i added the for condition.

Can anybody see where i went wrong by any chance ?

$(document).ready(function() {
  for(i = 1, i < 2, i++) {
    $("#info_abonnement" + i).hover(function() {
      var pos = $(this).position();
      var width = $(this).outerWidth();
      $("#info_abonnements" + i).css({
        position: "absolute",
        top: pos.top + "px",
        left: (pos.left + width) + "px"
      }).show();
    }, function() {
      $("#info_abonnements" + i).hide()
    });
  }
});
Jackymamouth
  • 159
  • 2
  • 11
  • 3
    foreach (i=1,i<2,i++){ is not valid sintax – juvian Jan 27 '16 at 17:51
  • 2
    Also, note, due to *closures*, the value of `i` will be the same (2) in each hover function. – Mike Christensen Jan 27 '16 at 17:54
  • 1
    Seems like you're confusing [`for`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) with [`foreach`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) – j08691 Jan 27 '16 at 17:55
  • Yes my bad, error while rewriting code, didn't work with for either – Jackymamouth Jan 27 '16 at 18:02
  • @Jackymamouth look at Mike_Christensen's comment, then maybe try to do it all with css like http://stackoverflow.com/questions/5199154/css-lighten-child-elements-on-parent-mouseover – depperm Jan 27 '16 at 18:04
  • @MikeChristensenah okey have you any suggestions ? – Jackymamouth Jan 27 '16 at 18:10
  • @depperm So you suggest that i should change z-index or something ? seems more complicated :/ – Jackymamouth Jan 27 '16 at 18:11
  • @Jackymamouth if you give your child elements a similar class then you should be able to do everything with just css like my earlier comment mentions – depperm Jan 27 '16 at 18:16

2 Answers2

1

Your foreach is wrong it should be something like:

for (i=1;i<2;i++){
depperm
  • 10,606
  • 4
  • 43
  • 67
0

You have not declared i. It should be:

var i;
for(i=1;i<2;i++)
P. Jairaj
  • 1,033
  • 1
  • 6
  • 8