In jquery how to get the values of a particular row of a table that is selected using checkbox?

16,965

Solution 1

Try this:

var stringresult = '';
$('#add').on('click', function () {
    $('input:checked').each(function () {
        $this = $(this);
        var one = $this.parent().siblings('td').eq(0).text();
        var two = $this.parent().siblings('td').eq(1).text();
        var three = $this.parent().siblings('td').eq(2).text();
        alert(one + ' ' + two + ' ' + three);

        //or just 
        stringresult += $this.parent().siblings('td').text();
    });
    console.log(stringresult);
});

DEMO HERE

Solution 2

If you want the strings in <td>s, here is jQuery code for that:

var str = "";
$('#add').click(function(){
    $('input:checkbox:checked').filter(function(){
        str = $(this).closest('tr').text();
    });
});

DEMO

Solution 3

Please see my fiddle for solution

[http://jsfiddle.net/a4WMc/]

http://jsfiddle.net/a4WMc/

Solution 4

Try this:

$('#alertTyp').val()
Share:
16,965
user2077648
Author by

user2077648

Updated on July 11, 2022

Comments

  • user2077648
    user2077648 almost 2 years

    This is my jsp:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Insert title here</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
    </script>
    
    </head>
    <body>
    <table id="one" style="border:1px solid red;">
        <caption>Table 1</caption>
    <thead>
    <tr>
    <th></th>
    <th >ID</th>
    <th> Name</th>
    <th>System</th>
    </tr>
    </thead>
        <tbody>
          <tr>
            <td><input type="checkbox" /></td>
            <td>12</td>
            <td>Sam</td>
            <td>FSS</td>
         </tr>
    
         <tr>
            <td><input type="checkbox" /></td>
            <td>87</td>
            <td>Harry</td>
            <td>MSS</td>
         </tr>
         <tr>
            <td><input type="checkbox" /></td>
            <td>23</td>
            <td>Rita</td>
            <td>MVV</td>
         </tr>
         <tr>
            <td><input type="checkbox" /></td>
            <td>65</td>
            <td>Tom</td>
            <td>RDD</td>
         </tr>   
       </tbody>
    </table>
    
    <br><hr><br>
    <button id="add">Add</button>
    </body>
    </html>
    

    Here, when I click on add button I want to get all the values of the corresponding row that is checked in different variables namely id, name & system that should contain the checked values.

    I want these values to be stored in a String (not map). Could you please suggest me a jquery / js to achieve the folllowing

    UPDATE

    If I have a hidden field along with the checkbox how can I get its value? For example

    <td>
    <input type="checkbox" />
    <input type="hidden" value="secret" id="alertTyp" />
    </td>
    
    • Arun P Johny
      Arun P Johny over 10 years
      if multiple checkboxes are selected then what should happen
    • user2077648
      user2077648 over 10 years
      I mean to say that ever td has a hidden value in it then how do I retrieve those hidden values?
    • Arun P Johny
      Arun P Johny over 10 years
      if the checkboxes in tr 1 and 2 are checked what is the output you re looking for