-1

I am trying to make operations(delete and edit) using javascript for each element of list. I took list from mysql database. The javascript function is returning just last value of list. enter image description here

This is php back-end

do{
     echo " <li class='list-group-item note'><div><strong>".$fetch['title']."</strong>
     <small>".$fetch['notedate']."</small></div>
     <div>
     <button type='button' class='btn btn-outline-primary' id='edit' value=".$fetch['id'].">&#9998;</button>
     <button type='button' class='btn btn-outline-danger' onClick='deleteData()' id='delete' value='".$fetch['id']."'>&times;</button>
     </div>
     </li>";
    }
    while ($fetch = mysqli_fetch_array($mysqli_query));

javascript:

function deleteData (){
var acc = document.getElementById("delete");
alert (acc);  
}

I want javascript function return value of button individually. For exampe: if I click the buton with value=2 javascript should return 2, if 3 then 3.

1
  • Unless you have a specific reason to deliberately use a do-while() loop to iterate your result set, it is probably a bad idea because it fetches the row AFTER each iteration (assuming a row exists). Commented Oct 27, 2019 at 19:57

1 Answer 1

0

This should work. Also you'll want to set unique values for "id"

do{
         echo " <li class='list-group-item note'><div><strong>".$fetch['title']."</strong>
         <small>".$fetch['notedate']."</small></div>
         <div>
         <button type='button' class='btn btn-outline-primary' id='edit' value=".$fetch['id'].">&#9998;</button>
         <button type='button' class='btn btn-outline-danger' onClick='deleteData(this.value)' id='delete' value='".$fetch['id']."'>&times;</button>
         </div>
         </li>";
        }
        while ($fetch = mysqli_fetch_array($mysqli_query));

then

    function deleteData (acc){
      alert (acc);  
   }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.