0

I keep getting "function is not defined" errors while working on a Javascript/HTML page.

EDIT: Updated Link: http://jsfiddle.net/Gmyag/134/

EDIT: Updated link http://puu.sh/8CxnC/b954c1c803.html is the actual one I'm working with and would likely prove leagues more useful than the fiddle.

HTML:

deliveryIdentification is the one giving issues. Code too long.
Had to add code block since I added a jsfiddle.
Sorry for not simplifying the example, but this is the first time I've seen this.

If I put everything on a separate script blocks others seem to work, but with the addDelRefOrder() since I need to declare var deliveryDummy[] before it throws "ReferenceError: deliveryDummy is not defined. And if I put deliveryDummy[] in the same block it says "ReferenceError:addDelRefOrder() is not defined".

As to why the structure is so weird it's due to it being a .jsp file. I'm just starting out with JSP and learning a lot along the way.

Any and all help as of how to fix this issue is greatly appreciated.

6
  • 1
    You have a error in your code you want to declare Array like this var deliveryDummy[]; but proper syntax for declaring array is this var deliveryDummy = []; Commented May 7, 2014 at 7:12
  • Thanks edited this as well and both the puush and the fiddle are updated. Issue not fixed so far. Commented May 7, 2014 at 7:38
  • 1
    One more error, you are using while (list.firstChild) but you never declared list any where in your code. Commented May 7, 2014 at 7:46
  • Yup caught that a bit ago and updated it. Thanks. Commented May 7, 2014 at 7:56
  • Changes done to HTML. Issue still unsolved. Commented May 7, 2014 at 8:22

3 Answers 3

2

You are defining function inside function ? Here

function renderList()
{
    // clean the list:
    while (list.firstChild) {
        list.removeChild(list.firstChild);
    }

    // Recreate li
    for(var i = 0; i < deliveryDummy.length; i++) {
        var entry = document.createElement("li");
        entry.appendChild(document.createTextNode(deliveryDummy[i]));
        var removeButton = document.createElement('button');
        removeButton.appendChild(document.createTextNode("Remove"));
        removeButton.setAttribute('onClick','removeName('+i+')');
        entry.appendChild(removeButton);
        list.appendChild(entry);
    }

    function removeDeliver(deliverIndex){
        deliverDummy.splice(deliverIndex,1);
        // Array changed to re-render List
        renderList();
    }

    function getDeliver() {
    return deliverDummy;
    }
}

you have renderList() and inside this two more funcs. This is the wrong structure for Javascript. Make separate functions.

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

4 Comments

Thanks for catching that. It's been 30 hours awake so far and I'm slipping a bit. I corrected the error in the push and will update the Fiddle shortly.
Put semicolon ; renderList() after this.
Done, puush updated as well. Not present in Fiddle must've deleted it at some point.
Updated it again. Big changes this time around, getting same error.
1

I'm not very familiar with jsp either but your HTML tagging is a little messy. Make sure that the html tags are properly nested.

Comments

1

in your javascript i noticed that you have made calls to functions before you have created them. first fix this issue and see if it resolves your problem.

window.onload=function addDelRefOrder()
{
    var deliveryVal = document.getElementById("deliveryIdentification").value;
    // Add to array
    deliveryDummy.push(deliveryVal); 
    // Array changed, Re-Render
    renderList();         /// <==== HERE
}

window.onload=function renderList() // <====== Function created here.
{
    // clean the list:
    while (list.firstChild) {
        list.removeChild(list.firstChild);
    }

    // Recreate li
    for(var i = 0; i < deliveryDummy.length; i++) {
        var entry = document.createElement("li");
        entry.appendChild(document.createTextNode(deliveryDummy[i]));
        var removeButton = document.createElement('button');
        removeButton.appendChild(document.createTextNode("Remove"));
        removeButton.setAttribute('onClick','removeName('+i+')');
        entry.appendChild(removeButton);
        list.appendChild(entry);
    }
}

1 Comment

Can you mention one such instance please? I can't spot it.

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.