Hi, I'm starting to learn Java EE - JSP / SERVLET and as for my learning progress, I'm trying to achieve this kind of web app but Im having problem with displaying the right selected row (TASK ID Column) from my table. It is populated from my oracle db. Im able to display my expected output correctly. But when I'm trying to get the selected row through clicking the corresponding button, I'm always getting the 1st row value, i.e. ID = '123456', no matter what Button I clicked, 2nd, 3rd, etc. It always display the 1st row. You can kindly see the image below. Hoping someone can guide me to my problem. Thanks.

Note: Apologies for my coding conventions and use of scriptlet, I may later use JSTL /EL after I got used to the basics of JAVA EE. Thanks.

EXPECTED OUTPUT:
7e4dda1aa1a2f58819a96efca9ea0a45

To check if its getting the correct value, I used javascript to retrieve the value of TaskID.

PARTIAL JSP CODE: I've cut some part of codes, displaying only TASKID and APPROVE Button for checking value purposes.

`<%
String strQuery = "SELECT * FROM SIM_TEST";

            try{
                Connection conn = ConnectionProvider.getConn();
                PreparedStatement ps = conn.prepareStatement(strQuery);
                ResultSet rs = ps.executeQuery() ;

                int status;
                String getId;


                    while (rs.next())
                    {
                      String taskId = rs.getString("TASKID");
              %>      
                      <tr>
                      <td><%=taskId%></td>

                      <td><input type="button" id="btnApprove" value="Approve" />
                          <input type="hidden" id="getTaskId" onclick="getTaskId();" value="<%=taskId%>" />
                      </td>
                      </tr>
              <%

                    }

                }catch(Exception e){
                    e.printStackTrace();
                }
            %>`

And to check if its getting the value of hidden type when APPROVE button is clicked, I use javascript.

<script type="text/javascript">
    function getTaskId(){
        var id = document.getElementById('getTaskId').value;
        alert(id);
    }
</script>

Correction for button action and hidden field value.

<td><input type="button" id="btnApprove" onclick="getTaskId();" value="Approve" />
<input type="hidden" id="getTaskId"  value="<%=taskId%>" />
</td>

Still cant get the specific row value from the table. Always prompting "123456". Any tips to overcome this?

hai batuzai04123 ,

why dont we try to pass taskid value to java script from button onclick eventt like as follows

<input type="button" id="btnApprove" onclick="getTaskId(<%=taskId%>);" value="Approve" />

if you try like as above there is no need of hidden feild as well as you may get the task id directly in java script also

try like this and let me know if you have any doubts in my clarification

happy coding

a big thanks radhakrishna, that code really work, but is there other way I could get the value using jquery, I cant seem to get the value of function gettaskid() inside the $(document).ready(function). Also, I realize its not really the way I should write javascript inside html tags, am I right? Tried already using jquery to get the taskid but it displays only the first row, but only your solution works! and Im lost at this point.

Thanks.

hai batuzai04123,

its so easy to get the same kind of thing using jquery for this you have to do the following things in your code

so let be your code like as follows (as previous)

<td>
<input type="button" id="<%=taskId%>" class="approve" value="Approve" />
<input type="hidden" id="Task<%=taskId%>" value="<%=taskId%>" />
</td>

then inside jquery place the following code

$(".approve").click(function(){
        var selecred_btn_id = $(this).attr("id");

        var sel_task_id = $("Task"+selecred_btn_id).attr("id");

        window.alert(sel_task_id);      
        // your remaing code goes here

})

you will get the approve button selected task id as you wish

i think you the point

check it once by doing as i explained above

let me know the status

note: i apologies to the members over here because i posted the code than explaining ( i feel beter by giving the code that explaining the concept)

hai members , sorry for my gramitical mistakes in some sentences of above post

I am facing same issue. ...this solution is not working

Please help...... when button is clicked from row.. each button is giving same id =1...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.