How to get the cell value by clicking on the button

I have a problem that I can not solve how much I try. By clicking the "Change Customer" button, I will not get the value from the CustomerNr cell. My problem is that I don’t know how to get the row index by clicking on the button, and then pass it to my function, so that I can specifically get CustomerNr in that row, which I clicked on. You can take a look at my jsfiddle link and note that this is the first time I have code in Javascript / JQuery. I am open to smart decisions.

Here you can see how far I have come. I managed to select a value from a specific cell.

function GetCellValues() {
    var Row = document.getElementById("somerow");
    var Cells = Row.getElementsByTagName("td");
    alert(Cells[0].innerText);

}

I managed to get the row index by clicking on td , but I want to get it by clicking the button.

function myMethod(obj) {
    alert(obj.parentNode.rowIndex); // parentNode is also used
}

- , #. ( #)

function GetCellValues(e) {
    //Something here
    alert(Cells[0].Rows[e] innerText);

}

http://jsfiddle.net/6srjc7qL/

+4
3

, , ...

, , , :

JavaScript:

    var a = document.getElementsByClassName('otherButton');

for (var i = 0; i<a.length;i++) {
    a[i].addEventListener('click',function(){

     var b = this.parentNode.parentNode.cells[0].textContent;
    alert(b);


    });


}

HTML:

<table id="somerow">
        <tr>
            <th>CustomerNr</th>
            <th>Name</th>
            <th>Contact</th>
        </tr>
        <tr >
            <td>1</td>
            <td>Cigarettes Inc</td>
            <td>Rambo</td>
            <td>
                <button class="otherButton" >Edit Customer</button>
            </td>
        </tr>
        <tr >
            <td >22</td>
            <td>Razor</td>
            <td>David</td>
            <td>
                <button class="otherButton">Edit Customer</button>
            </td>
        </tr>
        <tr>
            <td>3</td>
            <td>H&M</td>
            <td>Samuel Adams</td>
            <td>
                <button class="otherButton" >Edit Customer</button>
            </td>
        </tr>

}
+3

td, tr, 2 . :

function GetCellValues(obj) {

    alert(obj.parentNode.parentNode.rowIndex);

}
+2

HTML

    <html>

    <head>
        <style>
            table, td {
                border: 1px solid black;
            }
            .selected {
                background-color: yellow;
            }
        </style>
    </head>

    <body>
        <center>
            <table id="somerow">
                <tr>
                    <th>CustomerNr</th>
                    <th>Name</th>
                    <th>Contact</th>
                </tr>
                <tr>
                    <td>1</td>
                    <td>Cigarettes Inc</td>
                    <td>Rambo</td>
                    <td>
                        <button id="otherButton" onclick="GetCellValues()">Edit Customer</button>
                    </td>
                </tr>
                <tr>
                    <td onclick="myMethod(this);">22</td>
                    <td>Razor</td>
                    <td>David</td>
                    <td>
                        <button id="otherButton" onclick="GetCellValues()">Edit Customer</button>
                    </td>
                </tr>
                <tr>
                    <td>3</td>
                    <td>H&M</td>
                    <td>Samuel Adams</td>
                    <td>
                        <button id="otherButton" onclick="GetCellValues()">Edit Customer</button>
                    </td>
                </tr>
        </center>
    </body>

</html>

JS

function GetCellValues(elm) {
    alert(elm.parentNode.parentNode.cells[0].textContent);
}

function myMethod(obj) {
    alert(obj.parentNode.rowIndex);
}
+1

All Articles