23 Dec

jQuery Automatic Trim of Input Fields

Really common task with a really easy solution

$(function()
{
$(“input”).change( function() { $(this).attr(“value”, $.trim( $(this).attr(“value”) ) ) });
});

This attaches a function to the ‘change’ event of all <input> tags. When the input field is changed it uses the built in ‘trim()’ function of jquery to trim off whitespace.

Leave a Reply

Your email address will not be published. Required fields are marked *