JavaScript and why do uppercase letters sometimes work and sometimes not

In Notepad ++, I wrote a JavaScript file, and something did not work: a warning was shown when the button was pressed, but it did not work.

I used the autocomplete plugin that came with Notepad ++, which introduced me to onClick .

When I changed capital C to small C , it really worked.

So, first of all, looking at the autocomplete functions, I noticed a lot of functions that use capital.

But when you change getElementById to getElementById , you also get an error, and, even worse, my school reference book writes all capital letters, but all decisions are made in small letters.

So what is JavaScript and its selective nature in relation to which functions can have capital letters and which cannot?

+6
javascript dom html case-sensitive
source share
4 answers

Javascript is ALWAYS case sensitive, html is not.

It sounds like you think you're talking about whether the html attributes (like onclick, for example) or are not case sensitive. The answer is that attributes are not case sensitive, but a way to access them through the DOM. So you can do this:

 <div id='divYo' onClick="alert('yo!');">Say Yo</div> // Upper-case 'C' 

or

 <div id='divYo' onclick="alert('yo!');">Say Yo</div> // Lower-case 'C' 

but through the DOM you have to use the correct case. So this works:

 getElementById('divYo').onclick = function() { alert('yo!'); }; // Lower-case 'C' 

but you cannot do this:

 getElementById('divYo').onClick = function() { alert('yo!'); }; // Upper-case 'C' 

EDIT: CMS makes it a big point that most DOM methods and properties are in camelCase . The only exception that comes to mind is the properties of the event handler, and they are usually considered the wrong way to connect to events . Prefer to use addEventListener as in:

 document.getElementById('divYo').addEventListener('click', modifyText, false); 
+27
source share

Several IE objects are not always case sensitive, including some / most / all ActiveX - why should XHR.onReadyStateChange and XHR.onreadystatechange work fine in IE5 or IE6, but only the latter will work with its own XMLHttpRequest object in IE7, FF, etc. .d.

But, a short link for the " standard " API case:

  • UPPERCASE - constants (usually symbolic, since const is not supported globally)
  • Home - Classes / Object Functions
  • lowercase - Events
  • camelCase - everything else

There are no 100% guarantees. But, according to most, that's for sure.

+5
source share

JavaScript API methods are almost all called with lowerCamelCase names, and JavaScript is case sensitive

+1
source share

Javascript should always be case sensitive, but I saw cases in Internet Explorer where it wraps all uppercase letters for some function names, but not for others. I think this is limited to functions that also exist in Visual Basic, since there is an odd inbreeding between the interpreters. Obviously, this behavior should be avoided unless, of course, you intend to make code that works in only one browser :)

+1
source share

All Articles