Determine Last row in table

I need to be able to deterime whether or not a text input box is on the last row of a table or not.  Is there a way to find the row number or row index of a given input box within a html table?
LVL 3
steve918Asked:
Who is Participating?
 
BatalfConnect With a Mentor Commented:
This example will search for inputs in the last row of a table.

<html>
<body>


<table id="myTable">
    <tr>
        <td>Text</td>
    </tr>
    <tr>
        <td>Text</td>
    </tr>
    <tr>
        <td><input type="text" name="myText"></td>
    </tr>
</table>

<script type="text/javascript">
function findInput(){
    var obj = document.getElementById('myTable');
    var rows = obj.rows;
    var inputs = rows[rows.length-1].getElementsByTagName('INPUT');
    if(inputs.length>0){
        alert('input is on last row');
    }else{
        alert('No input on last row');
    }
}

</script>
<a href="#" onclick="findInput();return false">Find inputs</a>

</body>
</html>
0
 
archrajanCommented:
try this

var tbl = document.getElementById('urtableid') //id of ur table

var len = tbl.rows.length;

var temp = tbl.rows[len-1].innertHTML

if(temp.indexOf('<input type = "text">') != -1)
alert("input box found")
else
alert("No input box found in last row");
0
 
BatalfCommented:
I forgot to check if the <input> was of type text. It might not be needed either, but in case you have hidden fields or buttons, this is the modified version:

<html>
<body>


<table id="myTable">
    <tr>
        <td>Text</td>
    </tr>
    <tr>
        <td>Text</td>
    </tr>
    <tr>
        <td><input type="text" name="myText"></td>
    </tr>
</table>

<script type="text/javascript">
function findInput(){
    var obj = document.getElementById('myTable');
    var rows = obj.rows;
    var inputs = rows[rows.length-1].getElementsByTagName('INPUT');
    var inputFound = false;
    for(var no=0;no<inputs.length;no++){
        if(inputs[no].type.toLowerCase()=='text'){
            inputFound=true;
            break;
        }        
    }
    if(inputFound){
        alert('input is on last row');
    }else{
        alert('No input on last row');
    }
}

</script>
<a href="#" onclick="findInput();return false">Find inputs</a>
</body>
</html>
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
BatalfCommented:
steve918,

glad I could help.

Any reason why you graded the answer as a "B"? The EE grades aren't schoolgrade. If a comment helped you solve the problem, then you should always give an A grade. If not, then you should give the experts a chance to earn the "A" grade.

Batalf
0
 
steve918Author Commented:
Well, your solution was insightful, but wasn't what ultimatly lead me to my answer.  Get a g/f and spend less time one EE.  Nobody cares if you get a B.
0
 
73SpyderCommented:
I know this is from 3 years ago, but as a user of both this site and many...many DHTMLGoodies.. I'm extremely disappointed that someone who active sought help from the community would treat those who try to help this way.

It takes all kinds I suppose.  Anyway, I stumbled across this question in my search for Javascript help and it started me down the right path.  Thanks Batalf!!!

0
All Courses

From novice to tech pro — start learning today.