1

I ceate a JSP which fetch value from database and displayed it in a table. Now I want to create a click function for the table. Click the row it will display some value. I use a js code for it but when I click the line nothing happened. Can anyone help? Thanks.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="SCOfetch.*" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>First Page</title>
<% JCOtest connection1 = new JCOtest(); %>
    <%
        ArrayList<CompanyRecord> list = new ArrayList<CompanyRecord>();
        list = connection1.step4QueryTable();
    %>
//*This is the click function*
<script language="javascript">

function showBgc(idn){
alert (idn);
}

</script>    
</head>
<body>
    <c:set var="greeting" value="Hello, World!"/>

<!--    <img id="image-1" alt="" src="img/snow.jpg" width="300" height="300"/> -->
//This is the result table 
<table>
    <%
    int size=list.size();
    for(int i=0;i<size;i++){  
        CompanyRecord news =(CompanyRecord)list.get(i);  
        %>
        <tr>
        <td onclick="showBgc(i)"><%=news.getValue("Code") %></td>
        <td onclick="showBgc(i)"><%=news.getValue("Name") %></td>
        </tr><%
    }               
    %>              
</table>

</body>
</html> 

And I debug it in IE. the html is like

 <tr>
     <td onclick="showBgc(i)">DE01</td>
     <td onclick="showBgc(i)">Country Template DE</td>
 </tr>

and the error is i not defined.

kk luo
  • 549
  • 1
  • 9
  • 22
  • Please click edit, then `[<>]` and produce a [mcve] WITHOUT any JSP. This is not a JSP issue! – mplungjan Mar 12 '20 at 09:10
  • Does it show alert() that shows nothing? Or there is no alert() at all? – Alex - Tin Le Mar 12 '20 at 09:11
  • no alert() at all – kk luo Mar 12 '20 at 09:14
  • Have you checked the developer console of your browser? I suspect it will show an error in the lines of "Uncaught ReferenceError: i is not defined". – Ivar Mar 12 '20 at 09:14
  • Is your jsp code running without any errors ? – Sohail Ashraf Mar 12 '20 at 09:26
  • I debug in IE and from console the code is like CZ01 Country Template CZ and error is i undefined – kk luo Mar 12 '20 at 09:28
  • 2
    @kkluo See Sohail's answer below for the solution and see [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) for more information. (That article is about PHP, but it applies to Java/JSP's as well.) – Ivar Mar 12 '20 at 09:32

1 Answers1

0

You have to print the i value in the showBgc function call. Change showBgc(i) to showBgc(<%=i%>), otherwise the function will take i as a string value.

Example:

%>
    <tr>
        <td onclick="showBgc(<%=i%>)"><%=news.getValue("Code") %></td>
        <td onclick="showBgc(<%=i%>)"><%=news.getValue("Name") %></td>
    </tr> 
<%

To show code and Name.

<tr>
  <td onclick="showBgc('<%=news.getValue("Code") %>')"><%=news.getValue("Code") %></td>
  <td onclick="showBgc('<%=news.getValue("Name") %>')"><%=news.getValue("Name") %></td>
</tr>
Sohail Ashraf
  • 10,078
  • 2
  • 26
  • 42
  • Still not working. I guess the issus is about the parameter i definition? – kk luo Mar 12 '20 at 09:36
  • Now it worked.Thx.One more question if you don't mind. If I want to alert the value of Code and Name instead. What should I do? – kk luo Mar 13 '20 at 01:49