This is an article discussing on how to handle and reveal an error message caused by an improper declaration or used upon creating a web-based application using JQuery. It is an error message which is generated as stated in the title : “variable is not defined”. The variable itself refers to any variable name exist in the snippet code defined using JQuery.
As shown in the image above, the exact error message can be detailed out as follows :
ReferenceError: element_text is not defined
The problem is in a variable named ‘element_text’. After finding out part of the snippet code which is guessed will become the main cause for the error message to be appeared, below is the snippet code which is mentioned :
The snippet code itself is trying to add a list item HTML element to an unordered list HTML element as shown above. It is also trying to set it with several attributes such as value and text of the element itself. And the cause is actually because the variable named ‘element_text’ hasn’t been defined in the first place so that the error regarding element_text is not defined. So, to solve the problem regarding the error happened, just define the variable before the the snippet code stated above as follows :
var element_text = $("#list_selection option").filter(":selected").text();