开发者

Detect and remove Inputbox overflow

开发者 https://www.devze.com 2023-01-07 04:56 出处:网络
I have a input box with the attribute overflow:hidden; I have also used javascript to stopped text being entered so that it cant overflow however I would like to be able to remove any overflow if

I have a input box with the attribute

overflow:hidden;

I have also used javascript to stopped text being entered so that it cant overflow however I would like to be able to remove any overflow if the user manages to insert text into an already full textbox.

Is there anyway in javascript to detect when a texbox has hid开发者_StackOverflow中文版den overflow? and remove it

Thanks


if you want to activly trim the text in the input box using javascript you can try:

<html>
<head>
<script>
function clip()
{
    var maxLen = 3
    var box = document.getElementsByName('myInput')[0];
    if (box.value.length > maxLen - 1)
    {
        box.value = box.value.substring(0,maxLen - 1);
    }
}
</script>
</head>
<body>
<input name="myInput" onkeypress="clip()" />
</body>
</html>

this code is not pretty but it shows what you can do, every time the text in the text box gets to long the function trims the text(its a little stronger than what you asked but you can tone it down by changing the event to "onblur"). However, what etc suggested should usually work.


Are you trying to set the max number of characters that can be entered? If so:

<input type="text" maxlength="50" />
0

精彩评论

暂无评论...
验证码 换一张
取 消