Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling JavaScript function from PHP echo within a radio button

I have a series of radio buttons, which onClick will either reveal or hide a Div:

Reveal the Div:

<input type="radio" name="con[4]" value="1" onclick="toggleLayer4(this.checked);" id="con4" />

Hide the Div:

<input type="radio" name="con[4]" value="0" onclick="toggleLayer4(!this.checked);" checked="checked" id="con4" />

JavaScript:

function toggleLayer4(val)
        {
            if(val == '1' || val === true)
            {
                document.getElementById('con4').checked = true;
                document.getElementById('con4PSTN').style.display = 'block';
            }
            else if(val == '0' || val === false)
            {
                document.getElementById('con4').checked = false;
                document.getElementById('con4PSTN').style.display = 'none';
            }
        }

Now the problem, when the pag is recalled, I can get the radio button checked like this:

<input type="radio" name="con[4]" value="1" onclick="toggleLayer4(this.checked);" <? if ($conn_count[3] == 1){echo "checked=\"checked\"";}?> id="con4" />

But I need a away of calling the JavaScript function to reveal the div if the radio button is checked, I have tried to echo toggleLayer4(this.checked); within the PHP if statement inside tags, however this just seems to reurn the text in the html??

There must be a way, not really versed in JS.

Cheers, B.

like image 441
Bifterss Avatar asked Sep 20 '26 16:09

Bifterss


1 Answers

Here is a plain unobtrusive javascript for you which will save you some work - please notice there are no event handlers on the radios anymore.

I gave them unique IDs which is mandatory.

If you ever need to use jQuery for other stuff, this script can be a little more elegant.

I assume

<input type="radio" name="con[1]" value="1" id="con1_1" />
<input type="radio" name="con[1]" value="0" id="con1_0" />
<input type="radio" name="con[2]" value="1" id="con2_1" />
<input type="radio" name="con[2]" value="0" id="con2_0" />
<input type="radio" name="con[3]" value="1" id="con3_1" />
<input type="radio" name="con[3]" value="0" id="con3_0" />
<input type="radio" name="con[4]" value="1" id="con4_1" />
<input type="radio" name="con[4]" value="0" id="con4_0" />

and matching object to have conxPSTN where x is the number in the con[x]

window.onload=function() {
  var conLength = <?php echo count($con); ?>;
  for (var i=1;i<=conLength;i++) {
    var cons = document.getElementsByName("con["+i+"]");
    for (var j=0;j<cons.length;j++) {
      cons[j].onclick=function() {
        var id = this.id.split("_")[0];
        document.getElementById(id+"PSTN").style.display = (this.value==1)?"block":"none"
      }
      if (cons[j].checked) cons[j].click();
    }
  }
}
like image 130
mplungjan Avatar answered Sep 22 '26 06:09

mplungjan



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!