I would like to prevent additional input on a HTML table cell once it's width is reached.
The table has a fixed layout, no wrap and a specific width. The overflow is currently set to hidden.
Is it possible to filter (and ultimately prevent) input so that it stops at the fixed width so that there will be no overflow (hidden or otherwise). I am currently filtering carriage return using jQuery so that the cell will not expand to additional lines.
Perhaps I am asking too much from a HTML table...
To prevent cells (rows) from expanding vertically, you have to set a fixed height for table rows. Select the relevant rows and, on the Table Tools Layout tab, click Properties. On the Row tab, select "Specify height" and then choose "Exactly" for "Row height is." Specify the desired amount.
Syntax: white-space: normal|nowrap|pre|pre-wrap|pre-line; Example 1: This example uses white-space property to prevent cell wrapping using CSS.
"table-layout:fixed" in the style of the table itself is the only thing that worked for me. I agree "table-layout:fixed" solved my problem as well.
If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).
One possible solution is to copy the cell content on keydown
in an hidden field and to compute the value of this field:
$("table input").on("keydown", function(e) {
$("#copy").text(this.value);
var width = $("#copy").outerWidth();
console.log("w: " + width);
var code = e.keyCode ? e.keyCode : e.which;
if (width > 50 && code !== 8 && code !== 49) {
return false;
}
});
I have created a fiddle with such solution : http://jsfiddle.net/scaillerie/hnRjB/2/.
A point to be noted is that the font-family and font-size should be the same in the input
and in the div
in order to have the correct value, that is the reason of the CSS rule :
* {
font-family: arial;
font-size: 1em;
}
But instead of *
, you should restrict to suits your needs (for example, table input, #copy
).
EDIT :
Just note that with this code, the user can enter just more than the input length, but you can add some "long" character to your copied field, in order to test the width : http://jsfiddle.net/scaillerie/hnRjB/3/.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With