0

Hi i am trying to that a program that generate multiple click events with different ids depending on n-term. where n for now is 4,but it does not loop

here is my code

        for (var i=1 ;i<= 4;i++){       
            $('#rl+i').click( function () {
                $("#div"+i).fadeIn();
                $("#outer").fadeOut();

            });
      }  

my goal is to generate the below output if it is possible

            $('#rl1').click( function () {
                $("#div2").fadeIn();
                $("#outer").fadeOut();

            });

            $('#rl2').click( function () {
                $("#div2").fadeIn();
                $("#outer").fadeOut();

            });
                $('#rl3').click( function () {
                $("#div3").fadeIn();
                $("#outer").fadeOut();

            });
                $('#rl4').click( function () {
                $("#div4").fadeIn();
                $("#outer").fadeOut();

            });
Reginwaldt Led
  • 369
  • 3
  • 9
  • 19

2 Answers2

0

Try this:

for (var i = 1; i <= 4; i++) {
    $('#rl' + i ).click(function() {
        $("#div" + i).fadeIn();
        $("#outer").fadeOut();
    });
}
Kresimir Pendic
  • 3,597
  • 1
  • 21
  • 28
0

You are on the right track. Just modify the selector in the for loop to be

$('#rl'+i).click( function () {....

So the complete for loop is

   for (var i=1 ;i<= 4;i++){       
            $('#rl'+i).click( function () {
                $("#div"+i).fadeIn();
                $("#outer").fadeOut();

            });
      }  
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53