0

I am trying to retrieve data from specific rows in a table based on check box selection done by the user.

For this, I have found two methods eq(rowIndex) and nth-child(rowIndex). However,it works only when given an absolute values and not when its dynamic.

Have tried the following but no luck on this:

function getData()
 {
    var chkArr = document.getElementsByName('checkbox');
    var checkedRecord = 0;
    for(var i=0; i<chkArr.length; i++)
    {   
    if(chkArr[i].checked)
    {
              checkedRecord = i;
              $('#summaryTable > tbody  > tr').eq('#checkedRecord').children('td').each(function(j)
            //checkedRecord is the row index here
             {
             //fetch values from td
             }); 
          }
      }  
 }

Here's how my html looks

<table>
<tr>
    <td><input type="text" id="text1"></td>
    <td>
        <select>
            <option value="opt1">Orange</option>
            <option value="opt2">Green</option>
        </select>
    </td>
</tr>
<tr>..</tr>

Any suggestions on this?

8
  • can you please provide an example with also the table code? www.jsfiddle.net Commented Sep 5, 2013 at 12:51
  • 1
    Can you post your HTML so we can see what and where '#checkedRecord' is. Commented Sep 5, 2013 at 12:51
  • 1
    Remove last > in selector Commented Sep 5, 2013 at 12:51
  • 1
    eq() takes a number, not a selector. Commented Sep 5, 2013 at 12:54
  • 1
    fetch values from which td? Commented Sep 5, 2013 at 12:56

4 Answers 4

1

You want something like this:

$('#summaryTable > tbody  >  tr').eq(i).children('td')
    .each(function() { ... });

The argument to eq must be a number.

There's also no need for the checkedRecord variable, it's the same as i.

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

1 Comment

Can you make a fiddle that demonstrates the problem?
1

eq method need integer value as parameter, but you pass a selector syntax to this method. Look at your code at line:

$('#summaryTable > tbody  > tr').eq('#checkedRecord')....

You have define checkedRecord variable above as var checkedRecord = 0;. So you need to modify that line to become:

$('#summaryTable > tbody  > tr').eq(checkedRecord)....

Comments

0

try this,

var gridRows = $("#myTable tbody tr");
var qnt = gridRows[i].cells[2].childNodes[0].value;

and if you want all rows data so use this function

function GridCount() {
        var grid = $("#myTable tbody");
        var gridRows = $("#myTable tbody tr");
        var gridfootRows = $("#myTable tfoot tr");
        var subTotal = 0;
        var Id;
        var Name;
        for (var i = 0; i < gridRows.length; i++) {
            if (isFireFox()) {
                Id = gridRows[i].cells[0].childNodes[0].innerHTML;
                Name = gridRows[i].cells[1].childNodes[0].innerHTML;
            }
            else {
                Id = gridRows[i].cells[0].innerText;
                Name = gridRows[i].cells[0].innerText;
            }
            var qnt = gridRows[i].cells[2].childNodes[0].value;
            var price = gridRows[i].cells[3].childNodes[0].value;
            var total = parseFloat(price * qnt).toFixed(2);
            gridRows[i].cells[4].childNodes[0].innerHTML = total;
            subTotal = parseFloat(subTotal) + parseFloat(total);
        }

}
function isFireFox() {
   return navigator.appName == "Netscape";
}

3 Comments

why the mix of jquery and plain javascript?
problem in browser comparability so i am using this.
jQuery handles browser compatibility for you
0

I'm not exactly sure what you are asking for, but it seems you want to find the values of selects found in rows that correspond by index to checked checkboxes. This will allow you do do that:

function getData() {
    var rows = $('#summaryTable > tbody  > tr'), //cache rows for performance
        values = []; //array of values to return

    //iterate through all checkboxes
    $('input[type=checkbox]').each(function (i) { 
        if(this.checked) {
            //if checkbox is checked, find select and get value, then add to values array
            values.push(rows.eq(i).find('select').val()); 
        }
    });

    return values;
}

Comments

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.