0

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.

2
  • so you want the values of the checked checkboxes? Commented May 13, 2017 at 6:36
  • @ImmortalDude Yes. I want the value as well as the value of echoed variable. Commented May 13, 2017 at 7:02

1 Answer 1

1

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);
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

use $(this).attr('data-id') over $(this).data('id').. using attr is faster and also .data() will cache your results which is not required in this scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.