1

I have table . There are three columns : Name , Price and button "Make Order". I must get data from each row to my Javascript. Each input have id such as productName and productPrice . Table code:

<table border="2px">
<tr>
<th>Name</th>
<th>Price</th>
</tr>
<c:forEach items="${productList}" var="product" varStatus="status">
<tr>
    <form>
    <td> ${product.getProductName()}</td>
    <input type="hidden" id="productName" name="productName" value=${product.getProductName()}>
    <td>${product.getPrice()}</td>
    <input type="hidden" id="productPrice" name="productPrice" value=${product.getPrice()}>
    <td><input type="submit" onclick="makeOrder()" value="Make Order"></td>
    </form>
</tr>
  </c:forEach>
  </table>

And my js:

<script type="text/javascript">
function makeOrder() {
    var n=document.getElementById("productName").value;
    var p=document.getElementById("productPrice").value;
    alert("n="+n);
    alert("p="+p);
    //var win=window.open("/user/makeOrder?productName=" + n + "&productPrice=" + p);
}

But when I click on my button I always alert first row of my table . How can I alert other row ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nastya Gorobets
  • 197
  • 2
  • 10
  • 23

2 Answers2

1

Use a unique row id for each row and pass that id with makeOrder() function and then find child elements inside that row whose id is passed in function...

Here is working example....

function makeOrder(rowId) 
{
 var n=document.getElementById(rowId).childNodes[5].value;
    var p=document.getElementById(rowId).childNodes[9].value;
    alert("n="+n);
    alert("p="+p);
    //var win=window.open("/user/makeOrder?productName=" + n + "&productPrice=" + p);
}
<table border="2px">
 <tr>
 <th>Name</th>
 <th>Price</th>
 </tr>
 <tr id="row1">
  <form>
  <td>P1</td>
  <input type="hidden" id="productName" name="productName" value="P1">
  <td>$5</td>
  <input type="hidden" id="productPrice" name="productPrice" value="5">
  <td><input type="button" onclick="makeOrder('row1')" value="Make Order"></td>
  </form>
 </tr>
 <tr id="row2">
  <form>
  <td>P2</td>
  <input type="hidden" id="productName" name="productName" value="P2">
  <td>$6</td>
  <input type="hidden" id="productPrice" name="productPrice" value="6">
  <td><input type="button" onclick="makeOrder('row2')" value="Make Order"></td>
  </form>
 </tr>
</table>
Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20
0

You could call a function that gives all inputs with name productName, see below.

var n=document.getElementsByName("productName").value;
var p=document.getElementsByName("productPrice").value;
for(var i = 0; i < n.length; i++) {
    alert("row 1: " + n[i].value + p[i].value);
}

This should return all your rows. Each alert will show the data of 1 row.

namlik
  • 182
  • 1
  • 12
  • No, I dont need return all rows . I need return only row when I clicked button . – Nastya Gorobets May 03 '16 at 12:59
  • Ah, look into this answer, this way you'll get the button on which is clicked and from there you should be able to make your way to the data you want using siblings. http://stackoverflow.com/a/9012576/6270761 – namlik May 03 '16 at 13:05