Solved

Get clicked button row index of table

Posted on 2011-09-06
9
282 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
Comment Utility
You can specify your each row using a unique id so that you can find why id was clicked.
0
 

Author Comment

by:Sthokala
Comment Utility
can you please post some example code.

Thank you
0
 
LVL 14

Expert Comment

by:EMB01
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 14

Expert Comment

by:EMB01
Comment Utility
>>  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
Comment Utility
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
Comment Utility
Please show me the code on the "add" button.
0
 

Author Comment

by:Sthokala
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Do you want to insert HTML5 video into your site? This is the tutorial how to do so. What are the main advantages of HTML5 video? 1) Have good compression, good image quality, and low decode processor use. 2) It is royalty-free 3) It is easi…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

771 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

10 Experts available now in Live!

Get 1:1 Help Now