0

Im making a button 2d matrix in the format:

<table id="board">
        <tr class="row 0">
            <td class="col 0"><button></button><td>
            <td class="col 1"><button></button><td>
            <td class="col 2"><button></button><td>
        </tr>
        <tr class="row 1">
            <td class="col 0"><button></button><td>
            <td class="col 1"><button></button><td>
            <td class="col 2"><button></button><td>
        </tr>
        <tr class="row 2">
            <td class="col 0"><button></button><td>
            <td class="col 1"><button></button><td>
            <td class="col 2"><button></button><td>
</table>

I wanted to make one function when a user clicks on the buttons. My idea was to get the row and the column from the button that is pressed but Im not sure how to do that. In my js script I wanted to do something like:

document.querySelector('button').onclick = move("insert button row", "insert button column");

I would like any ideas on how to find the parent div or something capable of accomplishing this task.

5 Answers5

0

You can use parentNode to get the parent element of a particular element.

See example below.

document.querySelectorAll('button').forEach(function (e) {
  e.onclick = function () {
    console.log(`This is ${e.nodeName}`);
    console.log(`My dad is ${e.parentNode.nodeName}`);
    console.log(`My grandpa is ${e.parentNode.parentNode.nodeName}`);
  }
});
<table id="board">
        <tr class="row 0">
            <td class="col 0"><button></button><td>
            <td class="col 1"><button></button><td>
            <td class="col 2"><button></button><td>
        </tr>
        <tr class="row 1">
            <td class="col 0"><button></button><td>
            <td class="col 1"><button></button><td>
            <td class="col 2"><button></button><td>
        </tr>
        <tr class="row 2">
            <td class="col 0"><button></button><td>
            <td class="col 1"><button></button><td>
            <td class="col 2"><button></button><td>
  </tr>
</table>
josephting
  • 2,413
  • 2
  • 27
  • 32
0

You need to loop over all the buttons. And you have to assign a function to onclick, not call the function. Inside that function, you can use this to refer to the button that was clicked, parentElement to get the containing element, and rowIndex and cellIndex to get their positions in the table.

document.querySelectorAll('button').forEach(function(b) {
    b.addEventListener('click', function() {
        var col = this.parentElement.cellIndex;
        var row = this.parentElement.parentElement.rowIndex;
        move(row, col);
    });
});
Barmar
  • 596,455
  • 48
  • 393
  • 495
0

jQuery way (Needs JS lib inserted on page):

$('button').on('click', () => {
    this.parentNode.parentNode; //Retrieves TR tag of the individual button
    move("insert button row", "insert button column");
});

Non-jQuery Way:

document.querySelectorAll('button').forEach((e) => {
    console.dir(e); //Reads out of all properties and methods of element in console
  //Inserts click event listener logic if you please
  e.parentNode.parentNode; //Retrieves the TR tag of the button
    move("insert button row", "insert button column");
});
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Matthew
  • 2,887
  • 3
  • 14
  • 27
0

You can try to add the event listener to the tr element and then access the target and currentTarget's parent

for (var trEL of document.querySelectorAll('tr')) {
  trEl.addEventListener('click', event => {
  if (event.target.tagName === 'button') {
    move(event.target.parentNode, event.currentTarget)
  }
 });
}
jgutierrez
  • 26
  • 5
0

Below is one approach you could take.

The benefit of using closest() is you don’t have to worry about changing the markup in the future. I would also recommend using addEventListener() instead of onclick since you may inadvertently override another click event with onclick since it is in the global scope (see Why is using onClick() in HTML a bad practice?).

NOTE: closest() requires a polyfill in IE 11- (https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill)

const buttons = document.querySelectorAll("button");

buttons.forEach(el => {
  el.addEventListener("click", evt => {
    let button = evt.target;
    console.log({
      row: button.closest("tr").classList[1],
      columns: button.closest("td").classList[1]
    });
  });
});
button {
  display: block;
  height: 20px;
  width: 20px;
}

button:hover,
button:focus,
button:active {
  background-color: blue;
  cursor: pointer;
}
<table id="board">
  <tr class="row 0">
    <td class="col 0"><button></button><td>
    <td class="col 1"><button></button><td>
    <td class="col 2"><button></button><td>
  </tr>
  <tr class="row 1">
    <td class="col 0"><button></button><td>
    <td class="col 1"><button></button><td>
    <td class="col 2"><button></button><td>
  </tr>
  <tr class="row 2">
    <td class="col 0"><button></button><td>
    <td class="col 1"><button></button><td>
    <td class="col 2"><button></button><td>
  </tr>
</table>
Ted Whitehead
  • 1,544
  • 9
  • 17