kirangentlebreeze1987 Posted March 3, 2011 Share Posted March 3, 2011 hello experts,i am newbie to java scripti wanted to know how to get values of a particular row on button click each and every row contains a button and when the button of a particular row is clicked i should get the values of that row this is code i have written so far <script type="text/javascript">function addrow(){ var table=document.getElementById("mytable");var rowcount=table.rows.length;var row=table.insertRow(rowcount);/*create 1st cell*/var cell0=row.insertCell(0);var element=document.createElement("input");element.type="checkbox";cell0.appendChild(element);/*create 2nd cell*/var cell1=row.insertCell(1);cell1.innerHTML=document.getElementById("name").value;/*create 3rd cell cell*/var cell2=row.insertCell(2);cell2.innerHTML=document.getElementById("age").value;/*create 4th cell*/var cell3=row.insertCell(3);cell3.innerHTML=document.getElementById("desg").value;/*create 5th cell*/var cell4=row.insertCell(4);var element2=document.createElement("input");element2.type="button";element2.name="edit";element2.value="edit";cell4.appendChild(element2);element2.onclick=function editrow(){/* CAN I USE A STATEMENT LIKE THIS WHICH IS IN THE BELOW LINE... IS IT CORRECT? *//* alert(row[row.rowIndex].cell2.innerHTML); THIS STATEMENT IS NOT WORKING*/ alert(row[row.rowIndex].cell2.innerHTML); };/*create 6th cell*/var cell5=row.insertCell(5);var element3=document.createElement("input");element3.type="button";element3.name="update";element3.value="update";cell5.appendChild(element3);} help me experts Link to comment Share on other sites More sharing options...
dsonesuk Posted March 3, 2011 Share Posted March 3, 2011 As already mentioned you will have to find the outer parentNode (tr), then use childNodes, to show content of specific cell, and also 'this' called with the function on the click of the edit button, which will identify, which button was clicked in a particular row.But! as you are reading from inputs with specific id reference, which must be unique within a page, this won't be a problem as yet!. element2.onclick=function() { editrow(this)/* CAN I USE A STATEMENT LIKE THIS WHICH IS IN THE BELOW LINE... IS IT CORRECT? *//* alert(row[row.rowIndex].cell2.innerHTML); THIS STATEMENT IS NOT WORKING*/ //alert(row[row.rowIndex].cell2.innerHTML); };function editrow(editcell){alert(editcell.parentNode.parentNode.childNodes[2].innerHTML); // current button clicked : parent td : parent tr : 3rd childnode (td cells) : show innerhtml content } Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.