Fernando Ramos
Fernando Ramos

Reputation: 65

Check and Uncheck Checkbox

Why when I check the checkbox it works fine when I uncheck it nothing happen

Checked Unchecked

<form method="get">
    <table id="row">
        <tr><th colspan="2" >Location</th></tr>
        <tr><td>Country:</td><td><select id="country" name ="country"  style="width:200px"></select></td></tr>
        <tr><td>City:</td><td><select name ="city" id ="state"></select></td></tr>
        <script language="javascript">
            populateCountries("country", "state");
        </script>
        <tr><td></td><td><input onclick="myFunction()" type="checkbox" name="manualentry" value="manualentry" >Manual Entry</td></tr>
        <script>
            var table = document.getElementById("row");
            function  myFunction() {
                if (document.getElementsByTagName('manualentry').checked = true) {
                    document.getElementById("row").deleteRow(2);
                    var row = table.insertRow(2);
                    var cell1 = row.insertCell(0);
                    var cell2 = row.insertCell(-1);
                    cell1.innerHTML = "City:";
                    cell2.innerHTML = '<input  type="text" >';
                } else {
                    document.getElementById("row").deleteRow(2);
                    var row = table.insertRow(2);
                    var cell1 = row.insertCell(0);
                    var cell2 = row.insertCell(1);
                    cell1.innerHTML = "City:";
                    cell2.innerHTML = '<select name ="city" id ="state"></select>';
                }
            }
        </script>
        <tr ><td  colspan="2" align="right" > <input type="submit" value="Submit"></td></tr>
    </table>
</form>

Upvotes: 1

Views: 100

Answers (2)

JD Davis
JD Davis

Reputation: 3720

A couple things. GetElementsByTagName is the wrong function call, that method is used to get an array of elements by their actual HTML tag. Use GetElementsByName instead. Also, this call will return an array, so you need to specify which index it is (it will be index 0). Since checked is already a boolean value, you do not need to specify == true.

Replace if (document.getElementsByTagName('manualentry').checked = true)

with if (document.getElementsByName('manualentry')[0].checked)

Upvotes: 1

Tobias Sch&#228;fer
Tobias Sch&#228;fer

Reputation: 1358

You forgot a = in the if condition:

if (document.getElementsByTagName('manualentry').checked = true) {

try

if (document.getElementsByTagName('manualentry').checked == true) {

Upvotes: 0

Related Questions