Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my javascript not looping

I have the following code which works fine for the first row, but doesn't seem to loop through the table

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<table width="100%" border="0" cellspacing="2" cellpadding="2" id="demotbl">
  <tr>
    <th scope="col">Header 1</th>
    <th scope="col">Header 2</th>
  </tr>
  <tr>
    <td id="prodref">AA1</td>
    <td><input type="text" name="h_prodref" id="h_prodref"></td>
  </tr>
    <tr>
    <td id="prodref">BB1</td>
    <td><input type="text" name="h_prodref" id="h_prodref"></td>
  </tr>
  <tr>
    <td id="prodref">CC1</td>
    <td><input type="text" name="h_prodref" id="h_prodref"></td>
  </tr>
  <tr>
    <td id="prodref">DD1</td>
    <td><input type="text" name="h_prodref" id="h_prodref"></td>
  </tr>
  <tr>
    <td id="prodref">EE1</td>
    <td><input type="text" name="h_prodref" id="h_prodre5"></td>
  </tr>
  <tr>
    <td id="prodref">FF1</td>
    <td><input type="text" name="h_prodref" id="h_prodref"></td>
  </tr>
</table>
<p id="demo"></p>

<script type="text/javascript">
        var x = document.getElementById("demotbl").rows.length;
        document.getElementById("demo").innerHTML = "Found " + x + " tr elements in the table.";    
        var prodref = document.getElementById("prodref").innerHTML;
        var i = 0;
        do {
        document.getElementById("h_prodref").value = prodref;
        i++;
        }
        while (i < x);
</script>

</body>
</html>

My understanding (which is very basic) is that the code will look for a id called prodref and then copy the cell value to the text box, and work its way down until it has completed all rows.

like image 476
Jonathan Griffin Avatar asked Nov 05 '15 13:11

Jonathan Griffin


2 Answers

As mention above id must be unique. I create the following example using classes instead:

var x = document.getElementById("demotbl").rows.length;;
document.getElementById("demo").innerHTML = "Found " + x + " tr elements in the table.";
var prodref = document.getElementsByClassName("prodref");
var h_prodref = document.getElementsByClassName("h_prodref");
var i = 0;
for (i; i < prodref.length; i++) {
  h_prodref[i].value = prodref[i].innerHTML;
}
<table width="100%" border="0" cellspacing="2" cellpadding="2" id="demotbl">
  <tr>
    <th scope="col">Header 1</th>
    <th scope="col">Header 2</th>
  </tr>
  <tr>
    <td class="prodref">AA1</td>
    <td>
      <input type="text" name="h_prodref" class="h_prodref">
    </td>
  </tr>
  <tr>
    <td class="prodref">BB1</td>
    <td>
      <input type="text" name="h_prodref" class="h_prodref">
    </td>
  </tr>
  <tr>
    <td class="prodref">CC1</td>
    <td>
      <input type="text" name="h_prodref" class="h_prodref">
    </td>
  </tr>
  <tr>
    <td class="prodref">DD1</td>
    <td>
      <input type="text" name="h_prodref" class="h_prodref">
    </td>
  </tr>
  <tr>
    <td class="prodref">EE1</td>
    <td>
      <input type="text" name="h_prodref" class="h_prodref">
    </td>
  </tr>
  <tr>
    <td class="prodref">FF1</td>
    <td>
      <input type="text" name="h_prodref" class="h_prodref">
    </td>
  </tr>
</table>
<p id="demo"></p>

Example with your original html that I don't suggest using Document.querySelectorAll():

var x = document.getElementById("demotbl").rows.length;;
document.getElementById("demo").innerHTML = "Found " + x + " tr elements in the table.";
var prodref = document.querySelectorAll("#prodref");
var h_prodref = document.querySelectorAll("#h_prodref");
var i = 0;
for (i; i < prodref.length; i++) {
  h_prodref[i].value = prodref[i].innerHTML;
}
<table width="100%" border="0" cellspacing="2" cellpadding="2" id="demotbl">
  <tr>
    <th scope="col">Header 1</th>
    <th scope="col">Header 2</th>
  </tr>
  <tr>
    <td id="prodref">AA1</td>
    <td>
      <input type="text" name="h_prodref" id="h_prodref">
    </td>
  </tr>
  <tr>
    <td id="prodref">BB1</td>
    <td>
      <input type="text" name="h_prodref" id="h_prodref">
    </td>
  </tr>
  <tr>
    <td id="prodref">CC1</td>
    <td>
      <input type="text" name="h_prodref" id="h_prodref">
    </td>
  </tr>
  <tr>
    <td id="prodref">DD1</td>
    <td>
      <input type="text" name="h_prodref" id="h_prodref">
    </td>
  </tr>
  <tr>
    <td id="prodref">EE1</td>
    <td>
      <input type="text" name="h_prodref" id="h_prodref">
    </td>
  </tr>
  <tr>
    <td id="prodref">FF1</td>
    <td>
      <input type="text" name="h_prodref" id="h_prodref">
    </td>
  </tr>
</table>
<p id="demo"></p>

Also you have a typo here:

<tr>
    <td id="prodref">EE1</td>
    <td><input type="text" name="h_prodref" id="h_prodre5"></td>
  </tr>

id is h_prodref no h_prodre5.

like image 130
Alex Char Avatar answered Nov 13 '22 21:11

Alex Char


Here you go.

<!doctype html>
<html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
</head>

<body>
    <table width="100%" border="0" cellspacing="2" cellpadding="2" id="demotbl">
        <tr>
            <th scope="col">Header 1</th>
            <th scope="col">Header 2</th>
        </tr>
        <tr>    
            <td id="prodref1">AA1</td>
            <td><input type="text" name="h_prodref" id="h_prodref1"></td>
        </tr>
        <tr>
            <td id="prodref2">BB1</td>
            <td><input type="text" name="h_prodref" id="h_prodref2"></td>
        </tr>
        <tr>
            <td id="prodref3">CC1</td>
            <td><input type="text" name="h_prodref" id="h_prodref3"></td>
        </tr>
        <tr>
            <td id="prodref4">DD1</td>
            <td><input type="text" name="h_prodref" id="h_prodref4"></td>
        </tr>
        <tr>
            <td id="prodref5">EE1</td>
            <td><input type="text" name="h_prodref" id="h_prodref5"></td>
        </tr>
        <tr>
            <td id="prodref6">FF1</td>
            <td><input type="text" name="h_prodref" id="h_prodref6"></td>
        </tr>
    </table>

    <p id="demo"></p>

    <script type="text/javascript">
        var x = document.getElementById("demotbl").rows.length - 1;
        document.getElementById("demo").innerHTML = "Found " + x + " tr elements in the table.";
        var i = 0;
        do {
            var prodref = document.getElementById("prodref" + (i + 1)).innerHTML;
            document.getElementById("h_prodref" + (i + 1)).value = prodref;
            i++;
        }
        while (i < x);
    </script>

</body>
</html>
like image 2
Bagofjuice Avatar answered Nov 13 '22 21:11

Bagofjuice