Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Uncaught Reference error Function is not defined

Check the Fiddle to see the failure occurring.

When I add Data (Even if I leave it empty) to the text box and try to click "Add" it doesn't do anything.

Opening the Chrome and Firefox console both give me the same error, it says "changeText2()" is not defined.

How can I fix this? I've ran into this error several times and mostly it had really weird workarounds, but I am not sure as to the method for avoiding it or what I'm even doing wrong.

It seems removing the global variable declarations fixes it most of the time, however, I need them in this case and would rather know why and how this error occurs.

Any and all help is greatly appreciated.

Javascript:

var list = document.getElementById('deliveryIdArray'); var names = [];  function changeText2() {     var deliveryIdentification = document.getElementById('deliveryIdentification').value;     names.push(deliveryIdentification);//simply add new name to array;     //array changed re-render list     renderList(); }  function renderList(){     while (list.firstChild) {         list.removeChild(list.firstChild);     }     //create each li again     for(var i=0;i<names.length;i++){         var entry = document.createElement('li');         entry.appendChild(document.createTextNode(names[i]));         var removeButton = document.createElement('button');         removeButton.appendChild(document.createTextNode("remove"));         removeButton.setAttribute('onClick','removeName('+i+')');         entry.appendChild(removeButton);         list.appendChild(entry);     } }   function removeName(nameindex){     names.splice(nameindex,1);     //array changed re-render list     renderList(); }  function getDeliveries(){     return names; } 

HTML:

<b>Number(s): </b>     <input id = "deliveryIdentification" name = "deliveryIdentification" type = "text" size = "16" maxlength = "30">      <!-- Array Area Creation -->     <input type='button' onclick='changeText2()' value='Add' />      <ol id="deliveryIdArray">     </ol> 

Fiddle: http://jsfiddle.net/vSHQD/

like image 712
Erick Avatar asked May 15 '14 11:05

Erick


People also ask

How do I fix JavaScript reference error?

Reference errors in Javascript are mainly thrown when an attempt is made to reference a variable that does not exist or is out of scope. Therefore, in the majority of cases, a ReferenceError can be fixed by making sure that the referenced variable is defined correctly and is being called in the correct scope.

Why is JavaScript saying my function is not defined?

You're Calling the Function Before It's Defined If the code that calls your JavaScript function precedes that function's definition in your HTML document, you will come across the function is not defined error.

How do you fix function is not defined?

To clear a text input, assign blank string to 'value' attribute (not innerHTML attribute) document . getElementById("text"). value = ""; And don't call myfunction in JS tab If you use a <form> then an input with 'type' attribute "reset" will do the same thing.

When the function is not defined?

A function is not defined or is undefined if the value to be inputted is not in its domain. For instance, the domain of the function f(x)=√x f ( x ) = x is x≥0 x ≥ 0 . Getting its value at x=−2 is impossible as it is not defined at this point, i.e., it is not in its domain.


1 Answers

In JSFiddle, when you set the wrapping to "onLoad" or "onDomready", the functions you define are only defined inside that block, and cannot be accessed by outside event handlers.

Easiest fix is to change:

function something(...) 

To:

window.something = function(...) 
like image 106
Niet the Dark Absol Avatar answered Oct 16 '22 23:10

Niet the Dark Absol