0

Hey Guys in the below code every time the if statement is executed no matter what the condition is.

function addRow(tableID) {

            var table = document.getElementById(tableID);
            if((document.getElementById('select_degree').style.visibility = 'hidden')&&(document.getElementById('select_degree')!=null)){
              document.getElementById('select_degree').style.visibility = 'visible';
              document.getElementById('select_ratings').style.visibility  = 'visible';
             }

            else{ 
            var rowCount = table.rows.length;
            var new_row = table.rows[rowCount-1];  


            var row = table.insertRow(rowCount);


            var colCount = table.rows[0].cells.length;




            for(var i=0; i<colCount; i++) {

                var newcell = row.insertCell(i);


                newcell.innerHTML = new_row.cells[i].innerHTML;
            }
           }             
        }

When document.getelementById('add_degree') is null I get the error:

document.getElementById("select_degree") is null
addRow(tableID="add_degree")

But I already have a else statement if the element is null

2
  • If document.getElementById('select_degree') is null, then document.getElementById('select_degree').style.visibility will throw an error. If an error is thrown the whole script will terminate, the else branch will never be executed. Reverse the conditions. First check whether the element exists, and then assign (or compare) the other value. Does not make much sense to do it the other way round. Commented Feb 28, 2012 at 14:13
  • This is exactly why, when comparing against a literal, I make it a rule to have the literal as the LHS. Commented Feb 28, 2012 at 14:13

5 Answers 5

2

Your if statement is not using the correct operator. Replace your = (used for assignment) with the comparison operator: ==. In addition, you must check if the element exists before attempting to check it's properties. If you don't, you'll get an exception when the element doesn't exist.

Here is your code, corrected and cleaned up:

function addRow(tableID) {
    var objSelectDegree = document.getElementById('select_degree');
    if (objSelectDegree != null && objSelectDegree.style.visibility == 'hidden') {
        objSelectDegree.style.visibility = 'visible';
        document.getElementById('select_ratings').style.visibility = 'visible';
    }
    else {
        // Moved table var to else block - it was not used unless else was hit
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;
        var new_row = table.rows[rowCount - 1];
        var row = table.insertRow(rowCount);
        var colCount = table.rows[0].cells.length;

        for (var i = 0; i < colCount; i++) {
            var newcell = row.insertCell(i);
            newcell.innerHTML = new_row.cells[i].innerHTML;
        }
    }
}​
Sign up to request clarification or add additional context in comments.

2 Comments

That does not solve the overall problem though (which is checking the conditions in the wrong order). Assigning a value in a condition is not wrong per se, though probably unintended.
@FelixKling, you're right. I stopped looking once I saw the incorrect operator. Corrected.
2

This is why I love yoda conditionals. You're assigning a value (=) rather than evaluating it ==.


EDIT As suggested, I may point out that the order of checks in this particular statement is wrong. The check whether an element #select_degree exists should naturally occur before attempting to modify its properties.

However, given the element exists, the condition would still always evaluate true since the return value of the assignment (=) is used as an argument. As advocator for yoda conditionals, might I point out their advantage: for the following statement a syntax error would have been raised:

if ('hidden' = element.style.visibility)
{
// ...
}

2 Comments

Ah, that's why they switch the operands... Nice :)
Please have a look at the comments I made on the question and other answers. Correcting this (if it is unintended at all) does not solve the overall problem.
1

in your if statement:

'=' should be '==' for starters

then:

(document.getElementById('select_degree').style.visibility == 'hidden')
&&
(document.getElementById('select_degree')!=null)

you check a property before checking if it is null, if you switch them around the program will check null first, and if it is, drop to else, and not check the second condition.

in this case, even if it is null, it is going to access .style.visibility

Comments

0

style.visibility = 'hidden' -> style.visibility == 'hidden'

<CODE>
function addRow(tableID) {

   var table = document.getElementById(tableID);
   if((document.getElementById('select_degree').style.visibility == 'hidden') && (document.getElementById('select_degree')!=null)){
     document.getElementById('select_degree').style.visibility = 'visible';
     document.getElementById('select_ratings').style.visibility  = 'visible';
   }

   else{ 
     var rowCount = table.rows.length;
     var new_row = table.rows[rowCount-1];
     var row = table.insertRow(rowCount);                
     var colCount = table.rows[0].cells.length;

     for(var i=0; i<colCount; i++) {     
       var newcell = row.insertCell(i);                    
       newcell.innerHTML = new_row.cells[i].innerHTML;
     }
  }             
}

1 Comment

That does not solve the overall problem though (which is checking the conditions in the wrong order). Assigning a value in a condition is not wrong per se, though probably unintended.
0

At first check the null condition, this is basic law if something is present then proceed further , as in your case if element is not present it will throw undefined and nothing will work. even replace = with == in hidden part

Comments

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.