0

I have many tds .Every td has its id.When id is variables,it failed.When id is confirm(like A12),content will display.The OK code:

<script>
$(document).ready(function(){
  $('#A12').click(function (){
  $('#content').css('display','block'); 
  });
  });
</script>

the failed code:

<script>
 $(document).ready(function(){
   for(var i=1;i<9;i++)
   for(var j=1;j<21;j++)
   {    
     $('#Aij').click(function (){
     $('#content').css('display','block'); 
     });
   }     
});
</script>

I have tried one way,But unfortunately it failed again.

<script>
$(document).ready(function(){
for(var i=1;i<9;i++)
for(var j=1;j<21;j++)
{   
  var Aij=document.getElementById('A' + i + j); 
  var Bij=document.getElementById('B' + i + j);  
  var content=document.getElementById('content');

  $('#Aij').click(function (){
  $('#content').css('display','block'); 
  });
 }   
});
</script>

Then I tried '#A'+i+j,but the result always #A921.The code is:

<script>
$(document).ready(function(){
 for(var i=1;i<9;i++)
 for(var j=1;j<21;j++)
 {  
  $('#A'+i+j).click(function (){
  alert('#A'+i+j);
  $('#content').css('display','block'); 
  $('#selectID').val('#A'+i+j);
  });
 }   
});
</script>

Who can help me ?Aij should be A11 A12........A820

john
  • 55
  • 1
  • 7
  • You need to concatenate the variables in the selector string correctly - in fact, exactly as you already are in the `getElementById()` call – Rory McCrossan Jul 27 '17 at 09:32
  • Also, incremental `id` attributes is an anti-pattern. Put a class on all the elements, then apply a single handler to them: `$('.foo').click(function() { $('#content').show(); });`. Then there's no need for the ugly nested loop. – Rory McCrossan Jul 27 '17 at 09:34
  • @RoryMcCrossan,$('#A'+i+j).click(function (){}) can work, but '#A'+i+j is always A921.I don't know why? Can you help me ? – john Jul 28 '17 at 01:05

0 Answers0