1

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>

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

Comments

0

You forgot a = in the if condition:

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

try

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

1 Comment

With == nothing happen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.