Clear previously generated DOM <td> by id

Hi,
I have an if(){…}else{…} statement. The first part generates a table and includes an id attribute for a cell. When this function is called again the else part runs and this clears the inner.HTML of the cell which was given id in the previous part. But it isn’t clearing it.

<!DOCTYPE html>
<?php     
    include_once $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/func.inc.php'; 
    include_once $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/helpers.inc.php'; 
    include $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/arraybuilder.inc.php'; 
?>
<html lang="en">
      <head>
        <!-- <script src="/cuislegibney/js/searchJavaScript.js"></script> -->
        <link type="text/css" rel="stylesheet" href="/cuislegibney/css/stylesheet.css"/>
        <meta charset="utf-8">
        <title>Search</title>
      </head>
      <body>
      <link rel="icon" type="image/x-icon" href="http://www.cuislegibney.com/cuislegibney/images/favicon.ico"/>
      <header>
      <h1>Results archive</h1>
      </header>
        <div class="input">
            <p><a href="/cuislegibney">Return to Main Menu</a></p>
              <!-- <form action="?<?php htmlout($action);?>" method="post"> -->
              <form action="?" method="post">
                  <input type="hidden" name="action" value="search">
                  <input type="submit" value="<?php htmlout($button); ?>"> 
                <fieldset>
                    <legend>Search</legend>
                    <table id="examTables">
                        <tr>
                            <td>
                                <table class="inner" id="searchTable">
                                     <th>Exam Type:</th>                        
                                      <?php foreach($examSearch as $searchExam): ?>
                                           <tr><td class="left"><input type="checkbox" class="year" id="<?php htmlout($searchExam['name']); ?>" name="exams[]" value="<?php htmlout($searchExam['name']); ?>">
                                        <?php htmlout($searchExam['name']); ?></td></tr>
                                      <?php endforeach; ?>
                                </table>
                            </td>
                            <td>
                                <table class="inner" id="searchYear">
                                </table>
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </form>
        </div>
        <script language="javascript" type="text/javascript">

var rows = 0;

var checkboxesState = {};/*all need to be set to false initially*/
<?php foreach($exams as $element): ?>
type = <?php echo json_encode($element); ?>;
checkboxesState[type] = false;
<?php endforeach; ?>

function examTables(){
var genTable;
var genRows;
var type;
var exam = this.value;



var resultArray = [];
/* id needs to be replaced with a array or object which will be  record of the state of each checkbox*/
    if(checkboxesState[exam] == false){
            <?php foreach($exams as $element): ?>
                other = <?php echo json_encode($element);?>;
                if(exam == other)
                {
                    <?php if(!empty($subs[$element])): ?>
                        resultArray = <?php echo json_encode($subs[$element], JSON_PRETTY_PRINT); ?>;
                        genTable = true;
                    <?php else: ?>;
                        genTable = false;
                        alert("There are no results for this exam type currently in the database.");
                    <?php endif; ?>;
                }
            <?php endforeach; ?>;
            if(genTable)
                {
                var strOption = "";
                strOption = "<table>";
                var keys1 = Object.keys(resultArray);
                    keys1.forEach(function(item1){
                        var keys2 = Object.keys(resultArray[item1]);
                        keys2.forEach(function(items2){
                            var keys3 = Object.keys(resultArray[item1][items2]);
                                value = resultArray[item1][items2];
                                strOption += "<tr><td>" + value + "</td></tr>";
                        });
                    });
                    strOption += "</table>";

                    var table = document.getElementById("examTables");
                    var row = document.createElement("tr");
                    var cell = document.createElement("td");
                    cell.setAttribute("id", exam);
                    cell.innerHTML = strOption;
                    row.appendChild(cell);
                    table.appendChild(row);
                    checkboxesState[exam] = true;
                    rows++;
                }
        } else if(checkboxesState[exam] == true) {
            var cellA = document.getElementById(exam);
            cellA.innerHTML = ""; 
            checkboxesState[exam] = false;
        }
}

var checkboxes = document.getElementsByClassName('year');
    for(var index in checkboxes){
        //bind event to each checkbox
        checkboxes[index].onchange = examTables;
    }
    </script>
    </body>
</html>

I am using a checkboxesState object to keep track of the state of checkboxes. When a checkbox is checked a table (first part of the if else) is created and when that checkbox is unchecked I would like that table to be cleared (second part of the if else).

It is this line that doesn’t work,

var cellA = document.getElementById(exam);
 cellA.innerHTML = ""; 

Any help would be greatly appreciated,
Thanks

What does the generated HTML look like?

Hi,
First i need to ask about generated HTML!!!
I am using Firefox and shift-k gives me the console. Then clicking on the inspector gives me the uptodate HTML. But I can’t see it all in one go I have to keep expanding every element. Is there any way to do display all the html without having to go through every element?
The problem is that each of these little left arrows have to be clicked on to expand the HTML. Is there any way to just view it all?

I know there is already a sitepoint thread on this question.
Thanks

The HTML looks like this and not all the elements are expanded here, i.e. the

<head></head>
<body>
    <link href="http://www.cuislegibney.com/cuislegibney/images/favicon.ico" type="image/x-icon" rel="icon"></link>
    <header></header>
    <div class="input">
        <p></p>
        <!--

         <form action="?search" method="post"> 

        -->
        <form method="post" action="?">
            <input type="hidden" value="search" name="action"></input>
            <input type="submit" value="Submit"></input>
            <fieldset>
                <legend>

                Search

                </legend>
                <table id="examTables">
                    <tbody>
                        <tr>
                            <td>
                                <table id="searchTable" class="inner">
                                    <tbody>
                                        <tr></tr>
                                        <tr></tr>
                                        <tr>
                                            <td class="left">
                                                <input id="A2" class="year" type="checkbox" value="A2" name="exams[]"></input>

                                                    A2

                                                </td>
                                            </tr>
                                            <tr></tr>
                                            <tr></tr>
                                            <tr></tr>
                                            <tr></tr>
                                            <tr></tr>
                                            <tr></tr>
                                        </tbody>
                                    </table>
                                </td>
                                <td></td>
                                <td></td>
                            </tr>
                        </tbody>
                        <tr>
                            <td id="AS">
                                <table>
                                    <tbody>
                                        <tr>
                                            <td>

                                            Geography

                                            </td>
                                        </tr>
                                        <tr>
                                            <td>

                                            Mathematics

                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </form>
        </div>
        <script type="text/javascript" language="javascript">

        /*function subjectHandler() { var subjectA…

        </script>
    </body>
</html>

The page looks like,

What happends is that when I check a box a new table in generated below with the subjects for that exam type. The id of that cell is the exam type itself and by unchecking that checkbox again, the table with the subjects should disappear.

the script is

  <script>
  var rows = 1;
    var rem;
    var checkboxesState = {};
    <?php foreach($exams as $element): ?>
    type = <?php echo json_encode($element); ?>;
    checkboxesState[type] = false;
    <?php endforeach; ?>
    
    function examTables(){
    var genTable;
    var genRows;
    var type;
    var exam = this.value;
    
    var resultArray = [];
        if(checkboxesState[exam] == false){
                <?php foreach($exams as $element): ?>
                    other = <?php echo json_encode($element);?>;
                    if(exam == other)
                    {
                        checkboxesState[exam] = true;
                        <?php if(!empty($subs[$element])): ?>
                            resultArray = <?php echo json_encode($subs[$element], JSON_PRETTY_PRINT); ?>;
                            genTable = true;
                        <?php else: ?>;
                            genTable = false;
                            alert("There are no results for this exam type currently in the database.");
                        <?php endif; ?>;
                    }
                <?php endforeach; ?>;
    
                        if(genTable == true)
                            {
                            var strOption = "";
                            strOption = "<table>";
                            var keys1 = Object.keys(resultArray);
                                keys1.forEach(function(item1){
                                    var keys2 = Object.keys(resultArray[item1]);
                                    keys2.forEach(function(items2){
                                        var keys3 = Object.keys(resultArray[item1][items2]);
                                            value = resultArray[item1][items2];
                                            strOption += "<tr><td>" + value + "</td></tr>";
                                    });
                                });
                                strOption += "</table>";
    
                                var table = document.getElementById("examTables");
                                var row = document.createElement("tr");
                                var cell = document.createElement("td");
                                cell.setAttribute("id", exam);
                                cell.innerHTML = strOption;
                                row.appendChild(cell);//a cell needs to be added three times before a row is added.
                                //see previous example of cells being added into row and then row added to table.
                                rem = rows % 3;
                                //console.log("rows: " + rows);
                                    //if(rem == 2){
                                        table.appendChild(row);
                                    //}
                                
                                rem = rows % 3;
                                /*alert("rows: " + rows + " rem: " + rem);*/
                                rows++;
                            }
                            
        } else if(checkboxesState[exam] == true) {
            var cell = "";
            var table = document.getElementById("examTables");
            var row = document.createElement("tr");
            var cell = document.createElement("td");
            cell = document.getElementById(exam);
            empty = document.createTextNode("Changed");
            /*cell.innerHTML = "changed";*/
            cell.appendChild(empty);
            row.appendChild(cell);
            table.appendChild(row);
            checkboxesState[exam] = false;
        }
        alert("AS : " + checkboxesState["AS"] +  " A2 : " + checkboxesState["A2"] + " GCSE : " + checkboxesState["GCSE"] + " AP : " + checkboxesState["AP"] + " KET : " + checkboxesState["KET"] + " PET : " + checkboxesState["PET"] + " FCE : " + checkboxesState["FCE"] + " IELTS : " + checkboxesState["IELTS"]);
    }
    
    /*var cell = document.getElementById("testCell)");
    var celltext = prompt("Something here pleasde!");
    cell.appendChild(celltext);
    */
    
    function yearTable(){
        var exam = this.value; 
        var table = document.getElementById("searchYear");
        var row = document.createElement("tr");    
        var cell = document.createElement("td");
        if (this.checked){
           var empty = document.createTextNode(exam);
        }
        cell.appendChild(empty);
        row.appendChild(cell);
        table.appendChild(row); 
    }
    
    var checkboxes = document.getElementsByClassName('year');
        for(var index in checkboxes){
            //bind event to each checkbox
            checkboxes[index].onchange = examTables;
        }
        </script>

This code is a bit changed from first post.
Thanks,
Shane

This was the problem. the id that i was setting is the same as the id already used for checkboxes. So I changed it to,

cell.setAttribute(“id”, “subs” + exam);

and to this for the moment,

} else if(checkboxesState[exam] == true) {
        cell = document.getElementById("subs" + exam);
        console.log(cell);
        var celltext = "Something here please!";
        cell.innerHTML = celltext;
        
    }

Thanks

Maybe easier, maybe not.
What I sometimes do (Firefox dev tools) is on the “outer-most tag” that I’m interested in, right-click copy outer html and paste it into a text editor

1 Like

Thanks but it is strange that there isn’t an option to have all tags expanded.
Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.