Emm Emm - 1 year ago 82
Javascript Question

Get Php variable in to Javascript function

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.

Answer Source

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)" />

  function check(e)
    document.getElementById(e.getAttribute('data-check')).checked = true;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download