Page 1 of 1

onclick error

Posted: Tue Nov 14, 2017 3:48 pm
by edalakiran
Hi,

can anyone explain me, i am writing some function in the "Onclick event" example
"viewLink = function viewLink(xxxxx, xxxx, xxx, xxx, xxx, xxx, xxxx) " like this and i wrote some function in the javascript like

var viewLink = function viewLink(xxxxx, xxxx, xxx, xxx, xxx, xxx, xxxx){
console.log("test")
}

but i am getting some error i don't understand " unexpected end of input"

any help would be appreciated.

Thanks
Kiran.

Re: onclick error

Posted: Thu Nov 16, 2017 5:00 am
by ZoeW
Firstly, you are missing the ; after console.log("test"), however, I'm not an expert on JavaScript, but I find this website useful: http://jshint.com/
Paste in your code and it will tell you what is wrong.

Re: onclick error

Posted: Thu Nov 16, 2017 8:31 am
by edalakiran
Hi, thank you for your reply, when i click even it is not entering into that function ,

Re: onclick error

Posted: Mon Nov 20, 2017 1:20 pm
by matt.denninghoff
I think you've only defined the function, and you haven't called it. When you write a line like this:

Code: Select all

var viewLink = function(/*params here*/){/*some code here*/}
Then that simply defines a function named viewLink. If you want to call the function after it's defined, then you'd need to add a call to it:

Code: Select all

viewLink(/*some arguments here*/);
However, I don't understand why you are bothering defining a function within the "onclick" and then immediately calling it. Why not just put some code inside the "onclick" and avoid defining a function. In your case, why not just put this as your "onclick" code:

Code: Select all

console.log("test");
You need to define a function if you are calling the same code from multiple events or locations. If you are reusing it, then you'd want to define it somewhere besides the "onclick" event--for example, in a custom.js file.