accessing checkbox using javascript

In the following example, we hide/unhide the table using checkbox. We access the checkbox using javascript. Also the code changes the textbox ‘txtname’ to readonly

<!DOCTYPE html>
    <title>Untitled document</title>
  <meta charset="utf-8" />

    <script language="javascript" type="text/javascript">

        function myfunction()
            var chk = document.getElementById("chkchoice1");
            var tbl = document.getElementById("tbl1");

            if(chk.checked == true)
                //   document.writeln("It is checked");
       = "visible";
                document.getElementById("txtname").readOnly = true;
                //document.writeln("it is not checked");
       = "hidden";
                //document.getElementById("txtname").disabled = "";
                document.getElementById("txtname").readOnly = false;

<form id="frm" name="frm" method="post" action="about.html">
    <input id="chkchoice1" name="chkchoice1" type="checkbox"
           value="chkchoice1val" onclick="myfunction();" />    
    <br /><br />
    <input id="txtname" name="txtname" type="text" value="pak" />
    <br /><br />

    <table id="tbl1" border="1" style="visibility:hidden">

    <input id="btn" name="btn" type="button" value="click" 
           onclick="myfunction();" />

    <input id="btn1" name="btn1" type="button" value="click"
           onclick="window.location.href = 'form1data.html';" />




Add a Comment

Your email address will not be published. Required fields are marked *