You can use a combination of setInterval
timer and oninput
event to determine when those fields have changed (if oninput
is fired then setInterval
can be cleared)
this exam uses jQuery UI for the highlight effect
example
// traditional setInterval to check if input changed...
var isPriceChanged = function() {
$('.search-input').each(function() {
var $that = $(this),
oldvalue = $that.data('oldvalue');
// initialize oldvalue
if (oldvalue === undefined) {
$that.data('oldvalue', $that.val());
} else {
// update/highlight fields if value has changed
if ($.trim($that.val()) !== $.trim(oldvalue)) {
highlightChangedFields($that);
$that.data('oldvalue', $that.val());
}
}
});
},
// check input fields every half sec
priceChangedTimer = setInterval(isPriceChanged, 500);
// *new html5* 'input' event
$('.search-input').bind('input', function() {
highlightChangedFields($(this));
// disable traditional way if this event is called
clearInterval(priceChangedTimer);
});
var timer = undefined,
highlightChangedFields = function($that) {
var $row = $that.closest('.basket-row')
$price = $row.find('.price-enabled'),
$subtotal = $row.find('.subtotal-price'),
$fields = $.merge($price, $subtotal);
// clear previous timers
clearTimeout(timer);
// update price values
// probably use some kind of money formatter
// such as: http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript
$price.text('£' + $that.val());
$subtotal.text('£' + $that.val());
// highlight price fields
$fields.stop(true, true).effect('highlight', {color: '#ffbc0a'}, 2000);
};