troubleshooting Question

Obtain Row Index on a table

Avatar of James Lopez
James Lopez asked on
JavaScript
2 Comments1 Solution78 ViewsLast Modified:
Hello, I want to get RowIndex on a td using buttons within a table. Is there any body can correct my code?

!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Page Title</title>
</head>
<body>
	<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<table id="myTable" style="background: #F0FFFF">
	<tr>
    	<td>
        	Ernesto
            <button>RowIndex</button>
        </td>
        <td>Tyson</td>
    </tr>
	<tr>
    	<td>
        	James
            <button>RowIndex</button>
            </td>
        <td>Tejada</td>
    </tr>
	<tr>
    	<td>
        	Mike
            <button>RowIndex</button>
            </td>
        <td>Arnaiz</td>
    </tr>
</table>
	<script>
      $('#myTable').on('click touchstart', 'button', function (e){
          alert(this.parentElement.rowIndex)
          e.preventDefault();
});
    </script>
</body>
</html>
ASKER CERTIFIED SOLUTION
NorieAnalyst Assistant
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros