views:

404

answers:

1

I've got an HTML textarea whose width is set to 100% of the browser window, using CSS.

How can I calculate how many columns of text fit within the textarea?

A: 

The DOM textarea object supports a method cols that can be used to get or set the number of column of the textarea.

var numCols = document.getElementById('myTextArea').cols

More info on Textarea at w3cschools.

beggs
not helpful - "cols" returns "-1" if the textarea is dynamically sized
jes5199
I think your best bet then is to use `document.getElementById("myTextArea").clientWidth)` and/or `document.getElementById("myTextArea").offsetWidth)` and divide by the various factors affecting your font width. Or check out http://extjs.com/deploy/dev/docs/?class=Ext.util.TextMetrics
beggs