Reputation: 1651
What is the meaning of this condition in java script?
How does it work?
function displayunicode(e){
var unicode = e.keyCode ? e.keyCode : e.charCode
alert(unicode)
}
Upvotes: 0
Views: 200
Reputation: 12961
in keypress
event in javascript you have this stuff, in some browsers you just have the e.keyCode
in your event object, which here is e
, and in some other browsers you have e.charCode
instead. both keyCode
and charCode
refers to the key which is pressed.
and as @SB. has pointed out:
e.keyCode ? e.keyCode : e.charCode
means exactly:
var unicode;
if (e.keyCode)
unicode = e.keyCode
else
unicode = e.charCode
and as I have said this piece of code wants to get the key which has been pressed in keypress
event.
Upvotes: 2
Reputation: 7618
It is just a simple inline if, have a look at this to understand how it works: How to write an inline IF statement in JavaScript?
Upvotes: 0
Reputation: 22914
Shorthand for:
var unicode;
if (e.keyCode)
unicode = e.keyCode
else
unicode = e.charCode
alert(unicode);
You can even write it as:
var unicode = e.keyCode || e.charCode;
Upvotes: 4