Jump to content

how to get row index on button click


kirangentlebreeze1987

Recommended Posts

hello experts ,i want to get the row index on button click(this is button is created dynamically inside the table's row)this is the code which i have written so far

function addrow(){	  var table=document.getElementById("mytable");var rowcount=table.rows.length;var row=table.insertRow(rowcount);var cell0=row.insertCell(0);var element=document.createElement("input");element.type="checkbox";cell0.appendChild(element);var cell1=row.insertCell(1);cell1.innerHTML=document.getElementById("name").value;var cell2=row.insertCell(2);cell2.innerHTML=document.getElementById("age").value;var cell3=row.insertCell(3);cell3.innerHTML=document.getElementById("desg").value;var cell4=row.insertCell(4);var element2=document.createElement("input");element2.type="button";element2.name="edit";element2.value="edit";element2.onclick=function editrow(){/*IS THIS THE CORRECT WAY TO ADD ONCLICK EVENT TO THIS BUTTON *//*HOW TO GET THE ROW INDEX WHEN THE BUTTON IS CLICKED};

Link to comment
Share on other sites

Walk back up the tree. If the button is the child of a table cell, then element2.parentNode.parentNode refers to the row; element2.parentNode.parentNode.rowIndex contains the value you want.BUTsince the function is attached to element2, the correct way to refer to element2 is to use the this keyword. Thusthis.parentNode.parentNode.rowIndexNote that all of this changes if the button is nested in one or more other elements inside the table cell.Looking at the system again, it might be possible to enclose the value of row directly into the function. If it works, that would be even more bullet-proof, because a change in parent-child relationships would not require a revision to the script. In other words:

element2.onclick=function editrow(){   alert(row.rowIndex); // see if this works};

Note carefully that row in this context is the name of your variable, not the type of element. If you had used a different name for the row element, that is the name you would enclose in the function. Also, this kind of thing would only work because row is being enclosed by an anonymous function. If you were binding the click event to a stand-alone function, the only way to make it work is with the this pattern.

Link to comment
Share on other sites

If DD's suggestion does not work, you could use this quick little function I wrote up to get a parent of specified tag name for an element. It certainly isn't perfect but it does what it's supposed to do:

function getParent(tagName, objElement, root) {	var tmpParent = objElement;		if (root === undefined) {		root = "body";	}	root = root.toLowerCase();		while (tmpParent.nodeName.toLowerCase() != tagName.toLowerCase()) {		tmpParent = tmpParent.parentNode;				if (tmpParent.nodeName.toLowerCase() == root) {			break;		}	}		return tmpParent;}

You would call it like this:currRow = getParent('tr', element2);

Link to comment
Share on other sites

Walk back up the tree. If the button is the child of a table cell, then element2.parentNode.parentNode refers to the row; element2.parentNode.parentNode.rowIndex contains the value you want.BUTsince the function is attached to element2, the correct way to refer to element2 is to use the this keyword. Thusthis.parentNode.parentNode.rowIndexNote that all of this changes if the button is nested in one or more other elements inside the table cell.Looking at the system again, it might be possible to enclose the value of row directly into the function. If it works, that would be even more bullet-proof, because a change in parent-child relationships would not require a revision to the script. In other words:
element2.onclick=function editrow(){   alert(row.rowIndex); // see if this works};

Note carefully that row in this context is the name of your variable, not the type of element. If you had used a different name for the row element, that is the name you would enclose in the function. Also, this kind of thing would only work because row is being enclosed by an anonymous function. If you were binding the click event to a stand-alone function, the only way to make it work is with the this pattern.

thank you
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...