0

I know this topic is frequently asked but I've tried many ways and nothing works for me. I have a lot of checkboxes with the id generated (in Php) by a variable like this:<input type="checkbox" id="<?php echo $id; ?>" >. That I want to do is call a Javascript function every time that a radiobutton (autogenerated) is clicked for that that checkbox with id="<?php echo $id; ?>"can be checked.

I tried with things like:

function check() {
    var id = '<?php echo $id; ?>';
    document.getElementById(id).checked = true;
}

I know that maybe I could do with Ajax but I don't know how. (Up to this point is obvious that you know that the radiobutton has onclick = "check ()").

If anyone can tell me what I can do is I appreciate a lot.

Emm
  • 91
  • 1
  • 7

2 Answers2

0

You can try something like this:

<input type="checkbox" id="checkbox-<?php echo $id; ?>" />
<input type="radio" data-check="checkbox-<?php echo $id; ?>" onclick="check(this)" />

<script>
  function check(e)
  {
    document.getElementById(e.getAttribute('data-check')).checked = true;
  }
</script>
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • Dude, you are a teacher. Works perfectly I changed only: `data-check=""` for `data-check=""`. – Emm Oct 24 '16 at 09:32
  • I mean you are a MASTER. Lol. I'm really grateful. Thank you for your time and for your great help! Have a nice day. – Emm Oct 24 '16 at 09:34
0

You can use below code for dynamically generated radio and checkbox

$("input[type='radio']").click(function() {
    $(this).closest('input[type="checkbox"]').prop('checked', true);
});
Bhavik
  • 495
  • 2
  • 10