Q:

I want to know the value of the button I pressed in a table

I have an array of buttons arranged in each row of a table. When I click on a certain button, I want to know the row number the clicked button is positioned.    
I add each time a row to existing table, where in each row there is a button. I tried to put each button in an array, so when I click on one of them, I will get the index number from the array. Didn't succeed  
var btns[];
var btn=document.createElement("BUTTON");
    btn.innerHTML="Edit";
    btns.push(btn);
    cell1.appendChild(btns[btn_num]);
    btn_num=btn_num+1;

I expect to get the row number, so I can edit a specific row in a table.

A:

You don't need to store button in memory var btns.  I see a couple of practical ways to do this:
Create button with a class name 

 
<button class="btn-edit" onclick="BtnEditClick(this)">Edit</button>
<button class="btn-edit" onclick="BtnEditClick(this)">Edit</button>
<button class="btn-edit" onclick="BtnEditClick(this)">Edit</button>

  <script>
    function BtnEditClick(clickedElement){
      var btns = document.getElementsByClassName("btn-edit")
      var result = -1
      for(var i=0;i<btns.length; i++) {
        if(clickedElement == btns[i]){
          result = i
        }
      }

      if(result != -1)
        alert("found " + result)
      else
        alert("not found")
    }
  </script>

Reference to the value using data-attr in html5

<button class="btn-edit" data-index="0" onclick="BtnEditClick(this)">Edit</button>
  <button class="btn-edit" data-index="1" onclick="BtnEditClick(this)">Edit</button>
  <button class="btn-edit" data-index="2" onclick="BtnEditClick(this)">Edit</button>

  <script>
    function BtnEditClick(clickedElement){
      var index = clickedElement.getAttribute("data-index")
      alert("click index: " + index)
    }
  </script>

Pass index directly to the function

<button class="btn-edit" data-index="0" onclick="BtnEditClick(0)">Edit</button>
  <button class="btn-edit" data-index="1" onclick="BtnEditClick(1)">Edit</button>
  <button class="btn-edit" data-index="2" onclick="BtnEditClick(2)">Edit</button>

  <script>
    function BtnEditClick(index){
      alert("clicked on item:" + index)
    }
  </script>

Yes there are still a few other ways. For myself most of the time I use the second approach that get the element ( the button object that get clicked ) and then read the data that I want from data-attr ( you can define any data-attr you want e.g data-index, data-id ). This is very convenient, you will find it useful because sometimes you might need more data than just an index of the element inside the list. I hope it helps

