12 Replies - 724 Views - Last Post: 25 June 2015 - 09:15 AM

undefined error in my code after the contents of my function

Posted 24 June 2015 - 11:33 AM

Hello,

I want to thank all that will help me with my related issue. I am working on becoming a front end developer. When i load my function, i get an undefined after the contents of my function. I will add the source code to the bottom.

Replies To: undefined error in my code after the contents of my function

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 11:42 AM

There is really no reason to use document.write these days. I also don't think it makes sense in the context of a click event. I think it may have an issue with the fact that it is using document.write. Try substitute document.write with the "alert" function and see what you get.

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 11:50 AM

Make sure you don't have a space in your .js file either. Like all filenames, you really shouldn't use spaces. It might not see your function in the js file because it can't find the JS file since it has a space

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 04:50 PM

Functions only do what you tell them to. If you pass a parameter to the function then you can write the code to do with that parameter whatever you want it to do. It's not looking for or printing a parameter right now so, no, just adding a parameter is not going to cause it to be printed, and you'll still see undefined because the function still isn't returning a value. There's a very basic introduction to functions in Javascript here:

Re: undefined error in my code after the contents of my function

Posted 25 June 2015 - 09:15 AM

I'm not sure what you "need" to do, I don't know what your goal is. If you're just trying to get output the easiest way is to use console.log statements and then check the browser's console for the output. If you want to change the content of an element then, yes, you would get a reference to the element and set innerHTML.