thanks_in_advance
thanks_in_advance

Reputation: 2743

Javascript does not fire

I have 2 versions of some javascript code below.

In version 1, when I call function addNewCell(tableID), I am able to fire the line alert("bingo")

In version 2, I added some more code. Now the line alert("bingo") does not fire. What gives? I did check and the parentheses balance out.

version 1

var counter = 1;
var limit = 100;


function isEven(value) {
    value = Number(value);
    if (value%2 == 0)
        return true;
    else
        return false;
}


function addNewCell(tableID){
     if (counter == limit)  {
          alert("You have reached the limit of adding inputs");
     }
     else {
          alert("bingo");

    counter++;

        }
}

version 2

var counter = 1;
    var limit = 100;


    function isEven(value) {
            value = Number(value);
        if (value%2 == 0)
            return true;
        else
            return false;
    }


    function addNewCell(tableID){
         if (counter == limit)  {
              alert("You have reached the limit of adding inputs");
         }
         else {
              alert("bingo");





            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            var lastRow = table.rows[rowCount-1];

            var lastRowCellsCount = lastRow.getElementsByTagName('td').length;
            // alternative var lastRowCellsCount = lastRow.cells.length;


           if isEven(lastRowCellsCount) {

                var newRow = table.insertRow(rowCount);
                var newCell0 = newRow.insertCell(0);

                newCell0.innerHTML = '<input type="text" size="76" onclick="addNewCell('initialTable')" name="myInputs[]">  <br><br>  <textarea rows="6" cols="76" name="myInputs[]"></textarea > ' ;




            }
          else {

             var newCell1 = lastRow.insertCell(1); 
             newCell1.innerHTML = '<input type="text" size="76" onclick="addNewCell('initialTable')" name="myInputs[]">  <br><br>  <textarea rows="6" cols="76" name="myInputs[]"></textarea > ' ;

            }

            counter++;

        }
}

Upvotes: 2

Views: 87

Answers (3)

thanks_in_advance
thanks_in_advance

Reputation: 2743

It turns out that instead of if isEven(lastRowCellsCount) I was supposed to use if (isEven(lastRowCellsCount)) (i.e., I was missing the parentheses)

Upvotes: 0

Eric Hartford
Eric Hartford

Reputation: 18012

Use Firebug to view the console and you can see errors.

Upvotes: 0

nicosantangelo
nicosantangelo

Reputation: 13726

You have a syntax error in your string:

newCell0.innerHTML = '<input type="text" size="76" onclick="addNewCell('initialTable')" name="myInputs[]">  <br><br>  <textarea rows="6" cols="76" name="myInputs[]"></textarea > ';

will throw an SyntaxError: Unexpected identifier

using \ to scape the '' will work:

 newCell0.innerHTML = '<input type="text" size="76" onclick="addNewCell(\'initialTable\')" name="myInputs[]">  <br><br>  <textarea rows="6" cols="76" name="myInputs[]"></textarea > ';

or, if you're trying to concatenate a variable (not defined in the code):

 newCell0.innerHTML = '<input type="text" size="76" onclick="addNewCell(' + initialTable +')" name="myInputs[]">  <br><br>  <textarea rows="6" cols="76" name="myInputs[]"></textarea > ';

As a plus, I suggest you take a look a this link that shows you how to debug Javascript code in the Chrome DevTools (you can use firebug if you like)

Upvotes: 4

Related Questions