Krish
Krish

Reputation: 421

Getting values of a particular row using jQuery

I have the following code which prints the values in a datatable.

<?php
foreach ($result as $val) {
?>
<input class="example" type="checkbox" value="yes" id="example" name="example"><?php echo $val->id; ?>
<?php } ?>

Now I want to send the value to the server when the checkbox is checked. I have the following code for the same, however I'm confused on how I can get the ID of the row which is echoed by PHP.

$(".example").change(function() {
var value = $('.example').attr('value');
console.log(value);
});

Thanks in advance.

Upvotes: 0

Views: 43

Answers (1)

Sagar Jajoriya
Sagar Jajoriya

Reputation: 2375

You can set an another attribute to the checkbox like :

<input type="checkbox" data-id="<?php echo val->id ?>">

Then your code will :

$(".example").change(function() {
    if($(this).prop("checked")) {
        var value = $(this).val();
        var id = $(this).data("id")
        console.log(value);
        console.log(id);
    }
});

Upvotes: 1

Related Questions