How to show borders of an interactive symbol?

How does stack overflow display interactive character boundaries? As with editing comments, it shows how many characters you have left, as well as a warning if there are too few of them.

I need this exact functionality in Ruby on Rails ... But don't know how to do this?

+3
source share
5 answers

I use the following JavaScript function to limit the maximum length in text areas

function checkLength(edit, maxlen) 
{
if (edit.value.length > maxlen) 
    edit.value = edit.value.substring(0, maxlen);

    document.getElementById('remaining').innerHTML = edit.value.length;
}

Associate this with your textarea onKeyDown and onKeyUp attribute:

onKeyDown = "checkLength(this, 100);"
+5
source
+12

onkeydown . , , , , .

: ?

+1

, - javascript, onkeypress event, :

mytextbox.value.length

- :

if (mytextbox.value.length > maxlimit) 
mytextbox.value = mytextbox.value.substring(0, maxlimit);
0

You can also use simple javascript event handling to display the number of characters for input elements. No server-side processing required.

This javascript catches the keystroke event for the "txt" text area and shows the number of characters in the "count" space.

See how it works http://aaron.oirt.rutgers.edu/myapp/root/charCount.html

<html>
<head>

<script>
function go() {
    var txt=document.getElementById("txt");
    txt.onkeydown = countTxt;
}
function countTxt() {
    var txt=document.getElementById("txt");
    var count=document.getElementById("count");
    count.innerHTML = txt.value.length+1; // count the character not shown yet ;)
}
</script>

</head>
<body onload="go()">

<h3>type in the text area and see the count change</h3>

<textarea id="txt" rows="8" cols="30"></textarea>
<br>
count: <span id="count"> 0</span>

</body>

The account can be disconnected from my + -1 - correction, which (if you really want it) is left to the reader.

0
source

Source: https://habr.com/ru/post/1707659/


All Articles