Home > front end >  I can't get the id of button from php to javascript
I can't get the id of button from php to javascript

Time:11-07

> This is my javascript code
            $("#heys").click(function(){

               var fruitCount = $(this).attr('data-fruit');
               console.log(fruitCount);

            });

this button is from My php code


            <td >


               <button  type="button" ><a href="approve?approvid='.$id.'">Approve</a></button>
               <button  type="button" id="heys" data-fruit="<?php echo $id ?>" >DECLINE</button>

               </td>




 



I want to get my button id from php file that were fetch and use it in javascript. Please Help. Thanks

CodePudding user response:

Maybe your script run before rendering HTML of PHP file

Could you try to wrap the Javascript code with document ready ?

$( document ).ready(function() {
  $("#heys").click(function(){
    var fruitCount = $(this).attr('data-fruit');
    console.log(fruitCount);
  });
});

CodePudding user response:

did you imported jquery module?

  • Related