public
Created — forked from sunny/provide_html5.js

  • Download Gist
provide_html5.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
// Attempt to make a drop-and-forget bunch of scripts that mimick some missing html5 goodies automatically
// Example:
// $(document).ready(function() {
// ProvideHtml5.autofocus()
// ProvideHtml5.datepicker()
// ProvideHtml5.forcenumber()
// })
 
var ProvideHtml5 = {
autofocus = function() {
if (!Modernizr.autofocus)
$('input[autofocus=""]').focus();
},
colorpicker = function() {
if (!Modernizr.inputtypes.color)
$('input[type=color]').colorpicker();
},
datepicker = function() {
var datetypes = 'date month week time datetime datetime-local'.split(/ /)
$(datetypes).each(function(i, type) {
if (!Modernizr.inputtypes[type])
$('input[type='+type+']').datepicker()
})
},
forcenumber = function() {
if (!Modernizer.inputtypes.number)
$('input[type=number]').forcenumber();
},
placeholder = function() {
if (!Modernizr.input.placeholder)
$('input[placeholder]').placeholder();
},
required = function() {
if (!Modernizr.input.required)
$('[required]').required();
},
all = function() {
for (f in this)
if (f != 'all')
this[f]()
}
}
 
$.fn.placeholder = function() {
return $(this)
.focus(function() {
var that = $(this);
if (that.val() == that.attr('placeholder'))
that.val('').removeClass('placeholder')
})
.blur(function() {
var that = $(this),
val = that.val(),
placeholder = that.placeholder();
if (val == '' or val == placeholder)
that.val(placeholder).addClass('placeholder')
})
.blur()
}
 
$.fn.forcenumber = function() {
return $(this).blur(function() {
var val = $(this).val(),
num = parseFloat(val, 10),
min = parseFloat($(this).attr('min')),
max = parseFloat($(this).attr('max'));
 
if (!val.match(/^-?[0-9]+(\.[0-9]+)?+$/))
return $(this).val(val.replace(/^.*?(-?[0-9]+(\.[0-9]+)?+).*$/, '$1'))
 
if (min && num < min)
return $(this).val(min)
 
if (max && num > max))
return $(this).val(max)
 
}
})
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.