I have a jsp file where i am retrieving data from database in the form of a table. My jsp is as below:
<%
ResultSet rs1 = stmt.executeQuery("select * from book where category='Fiction'");
%>
<h1><font size="3">
<form name="f1" id="f1" method="post">
<table border="1">
<tr>
<th>Name of the Book</th>
<th>Author</th>
<th>ISBN</th>
<th>Price</th>
<th>Publication</th>
<th>Description</th>
</tr>
<%
while(rs1.next())
{
%>
<tr>
<td id="bname"><%= rs1.getString(1) %></td>
<td id="aname"><%= rs1.getString(2) %></td>
<td id="isbn"><%= rs1.getString(3) %></td>
<td id="price"><%= rs1.getString(4) %></td>
<td id="pname"><%= rs1.getString(5) %></td>
<td id="desc"><%= rs1.getString(7) %></td>
<td><input type="button" value="Add to Cart" onclick="cart()"></input></td>
</tr>
<%
}
%>
The output of this will be in number of rows. When i click the button 'Add to Cart' it should pass the cell elements to the javascript function and thereby to a servlet through JS function.
the problem i am facing here is, as the id of every 'td' is same, though i click in the second row of he table, only the elements of the first row are passing into the javascript function.
Here is my javascript function:
function cart()
{
var bname = window.document.getElementById("bname").textContent;
var isbn = window.document.getElementById("isbn").textContent;
var price = window.document.getElementById("price").textContent;
alert(bname);
alert(isbn);
alert(price);
}
the above 'alert's are displaying the values of the first rows only though i click the button present in the second row.
I tried so many alternatives online but couldn't solve the problem.
id. Anidmust be unique within the document.