Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onclick or onClick?

I thought that binding the click event in javascript is done by using node.onclick, and Chrome/Firefox seem to agree with me, but I saw it written .onClick here 4 times by 3 people, so it can't be a typo and I doubt that it's a coincidence.

So, why are people writing onClick when it does not work?

UPDATE: There are two good explanations; I don't know which one of them is the most plausible, so I will accept the answer based on popular vote, tomorrow.

like image 526
Gabi Purcaru Avatar asked Dec 07 '10 19:12

Gabi Purcaru


People also ask

Is Onclick and Onclick same?

onClick will work in html, but if you're defining the handler in JS, you need to use the lowercased onclick. in XHTML, HTML attributes are case sensitive.

What's the difference between onclick ={ () => function ()} and onclick function?

The different is in first it is not executing the function , it is calling the function . In second , it is executing the function there only. At onClick you should not call the function, instead set a function reference.

What is meant by Onclick?

OnClick is an Attribute of a Button or Picture object. It contains an expression that gets evaluated when you click the button or picture. It enables the user to perform actions beyond simply evaluating variables.


2 Answers

Because some browsers (depending on the DOCTYPE) are tolerant of the inline onClick="something();" attribute...it seems to have spread a bit, even into JavaScript questions where it doesn't work, since case matters.

Also, specifically to stackoverflow...people using it in questions...well, most of the time they wouldn't be asking a question if their code worked :)

like image 157
Nick Craver Avatar answered Sep 18 '22 11:09

Nick Craver


@Nick Craver pretty much has it nailed down and has my vote; I just wanted to add my thought.

I think it's onClick is often used in conversation because it's a bit more readable, and as an old habit from those of us who predate all lowercase HTML. However, in code - both JavaScript and HTML, onclick is correct and the only way it should appear. Even if you're using an older HTML doctype, stick to lowercase. If you ever update to a more strict doctype, you'll be glad your code doesn't need to be checked for case.

like image 37
Surreal Dreams Avatar answered Sep 20 '22 11:09

Surreal Dreams