Checking if a dom element exists with JQuery [Byte sized tips]

This is a simple tip but one I feel makes my code a bit easier to read. I was never very pleased with the standard way of checking if a dom element exits in jquery: if($('#userName').length !== 0){ //do something with $('#firstName') } The solution I like is to create a very simple jQuery plugin to encapsulate this logic: // this extension reads better when selecting elements $.fn.exists = function () { return this .length !== 0; }; You can place this anywhere you like such as in a ‘utils.js’ file, so long as it loads after jQuery. Now your code would like so: ...

How to use jQuery .on() instead of .live()

One of the most used features of jQuery is the easy methods it provides to to attach event handlers to dom elements like this simple example: $('.submitButton').click(function() { validateForm(); }); It doesn’t get much easier than that. However, a lot of times we’ll want to attach events to elements that were loaded after the initial page load such as from the result of an ajax request. This is where the .live() method comes in really handy: ...

Getting started with PetaPoco and Postgres

I’m currently working on a project I’ve inherited that uses a Postgres sql backend and I was looking for an easy way to make writing our data access layer less time consuming and painful. My first thought was to use a micro-ORM like Massive but while I’ve heard some really great things about Massive, I felt it might be a tough sell to my team members who aren’t too comfortable with Expandos and its dynamic nature (I know, but change in baby steps I suppose). Then I came across PetaPoco and it seemed to fit the bill. Its basically a mico-ORM like Massive with built in support for Postgres except that it also works with POCOs (Plain old CLR Objects) and was pretty easy to get up and running with. ...

Allow pasting multiple lines in IE textbox

You may have noticed before that if you try to paste more than one line of text into a textbox in Internet explorer it will on only paste in the first line and disregard the rest. Firefox and Chrome on the other hand will automatically paste all lines of the text onto the one line of the textbox. This issue came up in one of the projects I’m currently working on where we wanted users to be able to paste in a list of ID numbers they wanted to run a search on. ...