28

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.

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/

asked May 15, 2014 at 11:14
3
  • 1
    Change the wrapping in the fiddle (menu at the left) to "No wrap - in head". Commented May 15, 2014 at 11:15
  • @dystroy Causes errors because elements don't exist yet ;) Commented May 15, 2014 at 11:16
  • this will helpful to you....stackoverflow.com/questions/17378199/… Commented May 15, 2014 at 11:20

3 Answers 3

98

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(...)
answered May 15, 2014 at 11:15
Sign up to request clarification or add additional context in comments.

1 Comment

This works. jsfiddle.net/vSHQD/1 updated fiddle showing it working. Thank you for the fast and correct answer.
5

Change the wrapping from "onload" to "No wrap - in <body>"

The function defined has a different scope.

answered May 15, 2014 at 11:22

Comments

1

If you are using Angular.js then functions imbedded into HTML, such as onclick="function()" or onchange="function()". They will not register. You need to make the change events in javascript. Such as:

$('#exampleBtn').click(function() {
 function();
});
answered Sep 6, 2017 at 5:02

1 Comment

Worked for me. Think it's Jquery vs Javascript mistake

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.