Skip to content

Instantly share code, notes, and snippets.

@peol
peol / jquery-selector-formatter.js
Created November 7, 2010 21:05
With this short snippet, you can utilize $.format('selector', val1, val2, ...).jQueryMethod1() instead of concating your dynamic strings, making them hard-to-read/debug/whatever
// It works kinda like the asp.net's String.Format, meaning that you use {n} templates,
// {0} = your first argument (after the selector), {1} your second (after the selector) etc.
!function($, undefined) {
$.format = function(selector) {
var sel, args = arguments;
sel = selector.replace(/{\d}/g, function(part) {
var i = parseFloat( part.replace(/{|}/g, '') );
return typeof i === 'number' ? args[i+1] || '' : '';
$(function() {
// Clone HTML5's placeholder attribute functionality
$('input[placeholder]').each(function() {
// Make sure we cache the selector to fasten up the process
var $t = $(this);
// Store the placeholder text and then set the element's value
// TODO: Check to make sure value is empty before setting here
$t.data( 'placeholder', $t.attr('placeholder') );
$t.val( $t.data('placeholder') ).addClass('placeholder');