Solved

Get clicked button row index of table

Posted on 2011-09-06
9
283 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
  • 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
0
 
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…

914 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now