Hey guys,

Can anyone tell me what does "parseInt(key.which,10)" do in this code?
This code works perfectly, but I don't know what does that key.which,10 do.
What I know so far is that parseInt changes String to Integer.


$(document).ready(function() {
    $(this).keydown(function(key) {
        switch(parseInt(key.which,10)) {
			// Left arrow key pressed
			case 37:
				$('img').animate({left: "-=10px"}, 'fast');
			// Up Arrow Pressed
			case 38:
				// Put our code here
				$('img').animate({top: "-=10px"}, 'fast');
			// Right Arrow Pressed
			case 39:
				// Put our code here
				$('img').animate({left: "+=10px"}, 'fast');
			// Down Arrow Pressed
			case 40:
				// Put our code here
				$('img').animate({top: "+=10px"}, 'fast');


parseInt() and parseFloat() are built in functions for parsing a decimal number out of a string or a number of a different base (radix). In this case, it is telling the interpreter to expect a string that looks like a base 10 number. If it is already a number, then it is returned as is.

The key.which variable is a property in the EventObject, which is named in the parameter of the event handler, key. which will be the key code of the key pressed in that instant.

The EventObject is not preserved or persistent, but transient since it only hangs on to the latest event, which is discarded upon the next one.


Cool! That's perfect thanks!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.