1

Run the snippet and click on Click here!, there will be an error saying:

{ "message": "Uncaught TypeError: ll_ctq is not a function",
"filename": "https://stacksnippets.net/js", "lineno": 15, "colno": 93 }

But I have a function ll_ctq() in my code.

var web = 'http://localhost/prjct/';
var ll_50_50 = 'll_50_50=';
var ll_ctq = 'll_ctq=';
var ll_dd = 'll_dd=';
var and = '&';
var play = 'play.php?';

function ll_ctq() {

  var check_ll_ctq = document.getElementById("ll_ctq"),
    classes = ['red_cross'];

  if (check_ll_ctq.classList.contains("red_cross")) {
    //	 alert("Yes");
  } else {
    // alert("No");
    document.getElementById("ll_ctq").classList.add("red_cross");

    document.getElementById("A1").setAttribute('class', 'timeout');
    document.getElementById("B1").setAttribute('class', 'timeout');
    document.getElementById("C1").setAttribute('class', 'timeout');
    document.getElementById("D1").setAttribute('class', 'timeout');
    var check_ll_ctq = document.getElementById("ll_ctq"),
      classes = ['red_cross'];
    var check_ll_50_50 = document.getElementById("ll_50_50"),
      classes = ['red_cross'];
    var check_ll_dd = document.getElementById("ll_dd"),
      classes = ['red_cross'];


    if (check_ll_ctq.classList.contains("red_cross")) {
      var ll_ctq_status = "yes";
    } else {
      var ll_ctq_status = "no"
    }
    if (check_ll_50_50.classList.contains("red_cross")) {
      var ll_50_50_status = "yes";
    } else {
      var ll_50_50_status = "no";
    }
    if (check_ll_dd.classList.contains("red_cross")) {
      var ll_dd_status = "yes";
    } else {
      var ll_dd_status = "no";
    }
    var location = web + play + ll_50_50 + ll_50_50_status + and + ll_ctq + ll_ctq_status + and + ll_dd + ll_dd_status;
    setTimeout(function() {
      window.location = location;
    }, 3000);
  }
}
<div id="play_head">
  <div>
  </div>
  <div>
    <button id="ll_ctq" class="button_cstm_ll" style="margin-right:45px" onclick="ll_ctq()">Click here!</button>
  </div>
  <div>
  </div>
</div>
<table id="test" class="table table-responsive table_cstm">
  <tbody>
    <tr>

      <td colspan="2">
        <div class="question1" style="justify-content: center; width: 100%;">1) Who was First Prime Minister of India?(15304)</div>
      </td>
    </tr>
    <tr>

      <td>
        <div id="A1">A) M K Gandhi
        </div>
      </td>
      <td>
        <div id="B1">B) Jawaharlal Nehru
        </div>
      </td>
    </tr>
    <tr>

      <td>
        <div id="C1">C) Rajendra Prasad
        </div>
      </td>
      <td>
        <div id="D1">D) Chandra Shekhar Azad
        </div>
      </td>
    </tr>
  </tbody>
</table>

Run the snippet and click on Click here!, there will be an error saying:

{ "message": "Uncaught TypeError: ll_ctq is not a function",
"filename": "https://stacksnippets.net/js", "lineno": 15, "colno": 93 }

But I have a function ll_ctq() in my code.

2
  • You don't need to re-declare variables with var every time you assign a value. One declaration is enough. Commented May 9, 2018 at 11:11
  • Any error in a JS function results to this message. In your case the error is the definition of a variable with same name of the function. Commented May 9, 2018 at 11:12

2 Answers 2

5

You define ll_ctq twice.

First with the function declaration, which is hoisted (which is why it is first even though it appears later in the code).

Second with var ll_ctq = 'll_ctq='; which overwrites it with a string.

So by the time you come to call it, it is a string and not a function.

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

Comments

1

Your code does create a function called "ll_ctq", but then the code declares a variable with exactly the same name, so the function is lost.

Functions and variables share the same namespace.

The function declaration is hoisted above the var declarations, so even though the function declaration appears after them it is actually interpreted first.

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.