Skip to content

Instantly share code, notes, and snippets.

@lkptrzk
Created August 24, 2012 15:39
Show Gist options
  • Select an option

  • Save lkptrzk/3452103 to your computer and use it in GitHub Desktop.

Select an option

Save lkptrzk/3452103 to your computer and use it in GitHub Desktop.
Use number of characters to set textbox width
function setWidthInChars(selector, numChars) {
$('body').append('<div id="testwidth"><span>&nbsp;</span></div>');
var w = $('#testwidth span').width();
alert(w);
$('#testwidth').remove();
$(selector).css('width', (w * numChars + 1) + 'px'); /* +1 is for rounding problems */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment