0

i have several tables with same class names , these tables are created from php code via loop . like this

<?php 
$tableCount = 100;
$i=rand(1,999);
for ($i = 1; $i <= $tableCount; $i++) {
echo "<table class='tables'>";
echo "<tr class='myTr'>";
echo "<td  class='myTD'>";
echo $i;
echo "</td>";
echo "</tr>";
echo "</table>";
}

so we have 100 tds with different content , the problem for me is here : when the loop creates on page . and when i want to get all innerHtml or html() of all Td's class the compiler brings me first html() of td .

my jquery code :

var getclass = $(".myTD").html();

when i want to change the contents or remove or somethings ; i can do this. for example : for changing all contents of all td's with same class :

 $(".myTD").html("changed contents");

but i want to list . all html() of all td's with same class my output should be like this :

lists  outputs


  1,     14
  2,     188
  3,     20
  4,     2
  5,     99
  6,     11
  .,      .
  .,      .
  100,    73

and to end . but . my input gets me firs content ;

how can i solve that . plz :(

note this i want to do this with jquery or javascript. no php or server side .

Alex
  • 77
  • 5
  • Check this out - https://stackoverflow.com/questions/3871547/js-iterating-over-result-of-getelementsbyclassname-using-array-foreach – fraggley May 14 '20 at 01:41

1 Answers1

1

Use .each() to loop.

$(".myTD").each(function(i) {
    console.log(i, $(this).html());
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thanks for you reply friend ,i think its true. but how can i list that instead of console.log in a new div? – Alex May 14 '20 at 01:48
  • You can do whatever you want in the body of the function. Like `$("#newdiv").append(...)` – Barmar May 14 '20 at 01:58
  • i have input. i want to : when $("#input").val() == $("#input").each(function() { $(this).html(); }); the td with founded val style will changes. but its not working .and when i tried with this : $("#input").val() == $(".myTD").html() , its brings me first value of td , – Alex May 14 '20 at 02:23
  • `var x = $("#input").val(); $(".myTD").each(function() { if ($(this).html == x) { do something }})` – Barmar May 14 '20 at 02:47