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 ?

1
  • But after button submit my page overload and adress string changes right . So it means that changed data don't pass to my js Commented May 3, 2016 at 12:43

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

6 Comments

Yes , it works . But I dont know how many rows will be in my table . I try this: <tr id=${product.getProductName()}> and <td><input type="button" onclick="makeOrder(document.getElementById('productName').value)" value="Make Order"></td> But I get the same result - only the first row get
use same at both places... for tr <tr id=${product.getProductName()} > for button <input type="button" onclick="makeOrder(${product.getProductName()})" value="Make Order">
I get error : Uncaught TypeError: Cannot read property 'childNodes' of null
inspect your html and see is id printed in makeOrder() for each button ??
print ${product.getProductName()} between the single qoutes like this makeOrder('PRODUCT_NANE');
|
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.

2 Comments

No, I dont need return all rows . I need return only row when I clicked button .
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. stackoverflow.com/a/9012576/6270761

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.