3

I am searching my Table Column for the string "None". It does this but I am unable to get the row number after. I am attempting to use the "rowIndex" attribute. Not sure why it is pulling "Not a Number" (NaN). Table is 50 rows 10 cols. I am assuming it may have to do with that I am pulling from a column instead of Row.

function F0416()                                                                   
{

var tab = document.getElementById('part1Table');
var l = tab.rows.length;
var s = '';
for ( var i = 0; i < l; i++ )
{var tr = tab.rows[i];
var cll = tr.cells[2];                                                              
s += ' ' + cll.innerText;
}

var y = (s.indexOf('None') != -1)                                                   
document.write(this.rowIndex + 1)
1
  • 1
    What is the value of "this"? Make sure it is the current row. Commented Mar 5, 2014 at 21:44

2 Answers 2

2

Instead of concatenating all the values of the column into a string and searching the string, you could instead test the value of each cell in the column for the value 'None'. Then you would know the row number from the loop counter, and you could halt the loop if you find it instead of iterating over every row.

It would look more like this:

for ( var i = 0; i < l; i++ ) {
    var tr = tab.rows[i];
    var cll = tr.cells[2];                                                              
    if(cll.innerText.indexOf('None') != -1) {
        document.write(i + 1);
        break;
    }
}

You could also return the value of the row instead of outputting it.

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

1 Comment

Sorry I haven't answered for a few days. I have had other obligations at work which took all my time. I went with "Surreal Dreams" who was right on. I just pasted in the code and it worked. I am sure the JQuery answer would have worked as well but I am not using it right now. I have only been scripting for about a week total hours now and just trying to get use to it. Thank you both for your answers.
1

I would recommend using a rich javascript library like JQuery.

Then given the following HTML:

<table>
    <tr><td>Row 1- Column 1</td><td>Row 1 - Column 2</td>
    <tr><td>none</td><td>Row 2 - Column 2</td>
    <tr><td>Row 3- Column 1</td><td>Row 3 - Column 2</td>
</table>

You can use the following to get all the rows containing none:

var rows = $('table tr').filter(":contains('none')");

Have a look at this Fiddle example.

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.