Jump to content

Some Doubts


dollar

Recommended Posts

1. Why we've to use two "=", like "==" . Can't we use one "=" ?2. Why we've to use, first letter of first word as small letter. Like getElementById, mainContent ? Is there any alternatives for this ? Can we use ''-" to separate multiple words, like get-element-by-id ?3. Which is correct, onclick="" or onClick="" ?

Link to comment
Share on other sites

1. Why we've to use two "=", like "==" . Can't we use one "=" ?
Of course you can, assuming that what you want to do is assign a value. The = and == operators do different things. There is also a === operator that does yet another thing. You can use any of them anywhere you want, but chances are only one of them is correct for the situation.http://www.howtocreate.co.uk/tutorials/javascript/operators
2. Why we've to use, first letter of first word as small letter. Like getElementById, mainContent ? Is there any alternatives for this ? Can we use ''-" to separate multiple words, like get-element-by-id ?
No, the - operator is the subtraction operator, it's for math, you can't use it in identifier names. The reason you write getElementById the way you do is just because that's the way it was defined. There is a predefined function called getElementById that applies to objects, so when you want to use that function you have to spell it correctly. You can define your own function called GetElementById or get_element_by_id if you want, and use that instead. If you want to use the predefined functions you need to spell them correctly though.
3. Which is correct, onclick="" or onClick=""
Actually, it depends where you use it. If I remember correctly, inside of an HTML tag you use the lowercase version:<button onclick="...">Inside Javascript, you use the camelCase version.document.getElementById("the-button").onClick = function() { alert("bah!"); }I think I've got that right, if someone else knows otherwise please correct me.
Link to comment
Share on other sites

I think I've got that right, if someone else knows otherwise please correct me.
I can't comment on the correctness of your statement other than that I only ever use lowercase for events, both inline in the HTML and in javascript.
Link to comment
Share on other sites

Do not use camelcase for handler attributes. I've used lowercase for so long that I decided to check. element.onClick = xyz will create an attribute called onClick, and if it points to a function, you can call it explicitly, but it will not function as a handler. If you DO define element.onclick, it does NOT overwrite element.onClick -- they'll exist side by side.This is Firefox behavior, anyway.Maybe it's a historical inconsistency. EVERY other DOM property is written in camelcase.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...