Universal jquery enter-key binding for any function

var enterkey = function (jquery_obj, func, args) {
 jquery_obj.bind('keypress', function(e) {
  if (e.keyCode==13 && e.shiftKey==false) {
   func.apply(this, args);

This little snippet of code is cleverly designed to allow you to bind any page, textbox, button, with a function that will trigger the moment the enter-key is pressed. Here’s an example:

// Bind the textbox to alert 'Potatomonkies!' when the enterkey is pressed
var potatofunc = function (food, animal, exclamation) { alert(food + animal + exclamation); } 

enterkey($("#textbox"), potatofunc, ['Potato', 'monkies', '!'])

Now, whenever the enterkey is pressed on any element with an id=”textbox”, an alert will show saying “Potatomonkies!”. Easy.

Screen shot 2013-08-30 at 5.43.40 PM

Break it down now.

jquery_obj.bind('keypress', function(e) {

This line is a jquery method for listening for a keypress. Jquery does the heavy lifting, all we need to do is confirm that the keypress is the ‘enterkey’.

  if (e.keyCode==13 && e.shiftKey==false) {

’13’ is the ascii code for carriage return, IE., the enterkey. It’s convention, when writing in a textbox, to allow the user to hold the shiftkey down when pressing enter to put in an actual new-line instead of submitting. If shift isn’t held down, e.preventDefault() is there to stop the newline from writing.

Now the fun part.

   func.apply(this, args);

function.apply() is a method for manually calling function(), while providing the arguments as a list.

var potatofunc = function (food, animal, exclamation) {
 alert(food + animal + exclamation);

The function potatofunc demands three arguments, yet our func.apply(this, args); has only two, and the first argument this appears to do nothing.

This is because the arguments passed to potatofunc was provided when we called bound the enterkey function to potatofunc, as shown below:

var enterkey = function (jquery_obj, func, args)

enterkey($("#textbox"), potatofunc, ['Potato', 'monkies', '!'])

The function enterkey, as the first argument, is given the jquery of items with an id=textbox; the function potatofunc which now triggers on the enterkey as second; and then args, which contains the text to be displayed, as a list of strings.

func.apply(this, args) automatically takes the list [‘Potato’, ‘monkies’, ‘!’] and expands them as the parameters given to potatofunc.

The this in func.apply(this, args) does… well… nothing. It can easily be written as:

func.apply("cats and dogs", args);

And it’ll still work just fine. I’ll leave it up to you to figure that out. 😉

Entrepreneur’s Crucible

Many wannabe entrepreneurs (lookin’ at myself) have grandiose visions of their finished products, complete with bells and whistles. Yet any programmer or engineer who has begun a project understands that the process is not a destination; that there is no final product, only a current iteration.

I was speaking with my friend Scott. We began discussing what it takes to create a project from start to finish. One observation we both agreed upon was that, for any project, the best route is often easiest.

Criticisms aside it’s preferable to have a product that works and then improve upon it over time. If you have a working prototype, much of the actual business aspects open up: You can build a user base, you can showcase your project and acquire shareholders, but most importantly, you can begin to believe that what you’re doing makes sense.

With this in mind we noted that every product out there has probably been invented in the minds of many hopeful businessmen; why they didn’t complete their product was because they lacked was an understanding of the steps necessary to finally create the product.

You see at every given moment there is a suite of tools available. In software there are libraries, languages, and third party plug-ins that can turn a complex problem into a simple one.

Imagine for example that you can create a successful web-application by stitching together a suite of libraries already available online, with minimal coding on your own part. This is the ideal, putting in minimal work for the greatest reward. Paradoxically, it’s also why it’s the least likely as somebody as lazy as yourself may have jumped on the bandwagon before you.

If you look at Apple’s business model over the last decade you can note that the basic concepts of their products have already existed: RIM released the first Blackberry in 1999, Palm introduced their web browsing phone Kyocera 6035 in 2001. Yet the Iphone continues to dominate the smartphone market with its intuitive operating system and reliable design. All it took was for Steve Jobs to wait until the touchscreen, microprocessor, and battery technologies were perfected, and he unveiled a product that can fit in your pocket, browse the web, take calls, and run virtually any app you write for it.

Its tempting to come up with an idea of what you want, and try and find the components to build it up, but this may be the wrong approach. What Jobs understood was the Entrepreneur’s Crucible: Every product is built from its parts; It is the entrepreneur’s task to understand the tools, their strengths and weaknesses, and decide the product that is best for those tools.