?
Solved

Get clicked button row index of table

Posted on 2011-09-06
9
Medium Priority
?
303 Views
Last Modified: 2012-05-12
Hi,
  I have a table with 4 rows and I have a button associated with each row. When I click on button, I need to get the rowindex of the row. Please let me know how can I do it.

Thank you
0
Comment
Question by:Sthokala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 36492988
You can specify your each row using a unique id so that you can find why id was clicked.
0
 

Author Comment

by:Sthokala
ID: 36494946
can you please post some example code.

Thank you
1
 
LVL 14

Expert Comment

by:EMB01
ID: 36494970
Are you trying to do this with javascript or php?  Where is the parameter getting passed to?  This is how you would do it in php:
<table>
<tr>
<td><a href="page.php?row=1">row1</a></td>
</tr>
<tr>
<td><a href="page.php?row=2">row2</a></td>
</tr>
<tr>
<td><a href="page.php?row=3">row3</a></td>
</tr>
<tr>
<td><a href="page.php?row=4">row4</a></td>
</tr>
</table>

Open in new window

Let me know if you need some more help.
0
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 

Author Comment

by:Sthokala
ID: 36495389
Hi EMB01,
  Thank you for your help. I am creating table using javascript. Attached is my code. When I click on the button, I need the row Index of the table. Please let me know how can I do it.


Thank you
function getproceduresBySelectedCriteria()

{

var xmlhttp = getxmlHttpRequest();
var procDesc = document.getElementById("proc_desc").value;
var modalityId = checkSelectListSelected('modality_item');
var retired = document.getElementById("retiredCheck").checked?1:0;


var strURL = "getProcByDescOrModality.jsp?procDesc=" + procDesc+"&modalityId="+modalityId+"&retired="+retired;
xmlhttp.open("GET", strURL,true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
	
	var searchTable =document.getElementById("searchResultsTable");
	var lastRow = searchTable.rows.length-1,  i;
	  for (i = lastRow; i >= 1; i--) {
		  searchTable.deleteRow(i);
	}
	var responseText = xmlhttp.responseText.split("<br>");
	for(var i=0;i<responseText.length-1;i++) {
	var procValues = responseText[i].split("<td>");
	var searchResTable = document.getElementById("searchResultsTable");
	var tblBody = document.createElement("tbody");

	  var row = document.createElement("tr");
	  row.id=procValues[0];
	  
	  var cell = document.createElement("td");
	  var procDesc = document.createTextNode(procValues[1]);
	  cell.appendChild(procDesc);
	  row.appendChild(cell);
  
	  var cell1 = document.createElement("td");
	  var modalityDesc = document.createTextNode(procValues[2]);
	  cell1.appendChild(modalityDesc);
	  row.appendChild(cell1);
  
	  var cell2 = document.createElement("td");
	  var bodyPartDesc = document.createTextNode(procValues[3]);
	  cell2.appendChild(bodyPartDesc);
	  row.appendChild(cell2);
	  tblBody.appendChild(row);
	  
	  var cell3 = document.createElement("td");
	  var retired = document.createTextNode(returnRetiredValue(procValues[4]));
	  cell3.appendChild(retired);
	  row.appendChild(cell3);
	  tblBody.appendChild(row);
	  
	  var cell4 = document.createElement("td");
	  var buttonnode= document.createElement('input');
	  buttonnode.setAttribute('type','button');
	  buttonnode.setAttribute('name','Add');
	  buttonnode.setAttribute('value','Add');
	  cell4.appendChild(buttonnode);
	  row.appendChild(cell4);
		
		cell4.onclick=function(){ addACRProcedure(row.rowIndex);createAssociatedACRProcRow(row.rowIndex);searchTable.deleteRow(row.rowIndex);};
  
  	row.onclick=function(){HighLightTR(this,'#c9cc99','cc3333');getSelectedTableRow(this)};
  
 	 searchResTable.appendChild(tblBody);
	}
  }
}
xmlhttp.send();

}

Open in new window

0
 
LVL 14

Expert Comment

by:EMB01
ID: 36495507
>>  I need the row Index of the table.

How do you need it?  Do you need it in URL form, like this http://www.mywebsite.com/mywebpage.php?this-is-my-row-id=1
0
 

Author Comment

by:Sthokala
ID: 36495698
Hi,
  Please find the attached image for the table data. When I click on add button on the first row..I need row index of that row..when I click on add button in the second row..I need row index of the second row.
Please let me know if this is not clear.
Thank you
clip.png
0
 
LVL 14

Expert Comment

by:EMB01
ID: 36495753
Please show me the code on the "add" button.
0
 

Author Comment

by:Sthokala
ID: 36495773
var cell4 = document.createElement("td");
        var buttonnode= document.createElement('input');
        buttonnode.setAttribute('type','button');
        buttonnode.setAttribute('name','Add');
        buttonnode.setAttribute('value','Add');
        cell4.appendChild(buttonnode);
        row.appendChild(cell4);
            
            cell4.onclick=function(){ alert(row.rowIndex)};
0
 
LVL 14

Accepted Solution

by:
EMB01 earned 2000 total points
ID: 36496087
I would try changing to this:
var cell4 = document.createElement("td");
        var buttonnode= document.createElement('input');
        buttonnode.setAttribute('type','button');
        buttonnode.setAttribute('name','Add');
        buttonnode.setAttribute('value',row.rowIndex);
        cell4.appendChild(buttonnode);
        row.appendChild(cell4);
            
            cell4.onclick=function(){ alert(row.rowIndex)};

Open in new window

This way, the button name is 'add' and the value is the index of the item which is to be added.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question