Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Display Specific value when I click Specific button

When I click all the button output will be same (i.e 1).I want to display specific value of specified button for EXAMPLE:- When I click button 1 then It must display output as 1 and when I click 2 it must display output as 2.please help me.

enter image description here

<?php
    for ($i=0; $i < 10 ; $i++) { 
    echo '<br><button value="'.$i.'" class="my_remark_btn">'.$i.'</button>';
 }
?>

<script type="text/javascript">

    $('.my_remark_btn').click(function(){
        remark_aftr_click();  
    });

     function remark_aftr_click(){
        var k = $(".my_remark_btn").val();  
         $.ajax({url: "my_remark_act.php",
             cache: true,
             type: "POST",
             data: {go:k},
            success: function(data_remark_mi){
                alert(data_remark_mi);
           }
         });
      }

</script>

my_remark_act.php:-

<?php echo $_POST['go']; ?>
like image 990
nirks Avatar asked Nov 23 '25 08:11

nirks


1 Answers

$('body').append("<button data-value='val' class='my_remark_btn'>val</button>")

$(document).on('click', '.my_remark_btn', function() {
alert($(this).attr('data-value'))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  1. Button has no value.
  2. Use data attribute like data-value.
  3. Get it like $(this).attr('data-value') using this context for clicked button.
  4. Use event delegation for dynamically added elements.
like image 64
guradio Avatar answered Nov 24 '25 23:11

guradio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!