views:

27

answers:

2

hi every one;

I want to get the parameter in my table(HTLML) table every time the user click edit button. The table contains in each row an edit button like following:

 retour.append("<td>");

                           retour.append("<button  id=edit name=edit type=button  onClick= editarow()>");
                           retour.append("<img src=edit.gif />");
                           retour.append("</button>");
                           retour.append("</td>");

to get the value of each row where edit is clicked using javascript I do the following:

 function  editarow(){
var table = document.getElementById("sheet");
    var buttons = table.getElementsByTagName("button");
       for(var i=0; i<buttons.length; i++) {
        if(buttons[i].name=="edit") {
            buttons[i].onclick = function() 
                {
                var buttonCell = this.parentNode;//cell
                var editThisRow = buttonCell.parentNode;//td
                var er=editThisRow.parentNode.attributes[1].value;
               alert(er);

}

} }

}

but i didn't get the values expected. thanks for help

+1  A: 

Your have an error in your button element, this:

onClick= editarow()

should be

onclick='editarow()'

(The "C" should be lower case, and the function call in quotes.)

Let's assume a row looks like this:

<tr>
    <td data-foo="bar">John</td>
    <td>Doe</td>
    <td>...your edit button...</td>
</tr>

In editarow, you can do this:

function editarow() {
    var row, firstNameCell, lastNameCell;

    // `this` is the button, so `this.parentNode` is the cell, and
    // `this.parentNode.parentNode` is the row
    row = this.parentNode.parentNode;

    // The first name cell is the first child
    firstNameCell = findElement(row.firstChild);

    // The second name cell is its next sibling
    lastNameCell = findElement(firstNameCell.nextSibling);

    // Their "values" are the text within them, most easily accessed via
    // `innerHTML`
    alert("First name is " + firstNameCell.innerHTML);
    alert("Last name is " + lastNameCell.innerHTML);

    // Or if you store data in attributes, you can get them via
    // `getAttribute`
    alert("data-foo = " + firstNameCell.getAttribute("data-foo"));
}

function findElement(node) {
    while (node && node.nodeType != 1) {
        node = node.nextSibling;
    }
    return node;
}

This is all made a lot easier if you use jQuery, Prototype, Closure, or other libraries. (For instance, the findElement function above is very crude; its purpose is to skip over text nodes and such.)

T.J. Crowder
yes it works well, thanks.
kawtousse
@kawtousse: Cool, glad that helped!
T.J. Crowder
A: 

so it isresolved like that:

function editarow() {
    var row, firstNameCell, lastNameCell;
    var table = document.getElementById("sheet");
    var buttons = table.getElementsByTagName("button");
    for(var i=0; i<buttons.length; i++) {
        if(buttons[i].name=="edit") {
            buttons[i].onclick = function() 
                {
       row = this.parentNode.parentNode;

    // The first name cell is the first child
    NameCell1 = findElement(row.firstChild);
    NameCell2 = findElement(NameCell1.nextSibling);
    NameCell3=findElement(NameCell2.nextSibling);
    NameCell4=findElement(NameCell3.nextSibling);
    NameCell5=findElement(NameCell4.nextSibling);
    NameCell6=findElement(NameCell5.nextSibling);
    NameCell7=findElement(NameCell6.nextSibling);

    // `innerHTML` pour obtenir la valeur
    alert("name 1  is " + NameCell1.innerHTML);
    alert("name 2  is " + NameCell2.innerHTML);
    alert("name 3  is " + NameCell3.innerHTML);
    alert("name 4  is " + NameCell4.innerHTML);
    alert("name 5  is " + NameCell5.innerHTML);
    alert("name 6  is " + NameCell6.innerHTML);
    alert("name 7 is " + NameCell7.innerHTML);

}
        }}
        }

function findElement(node) {
    while (node && node.nodeType != 1) {
        node = node.nextSibling;
    }
    return node;
}
kawtousse