Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
140
rated 0 times [  146] [ 6]  / answers: 1 / hits: 46041  / 12 Years ago, thu, september 13, 2012, 12:00:00

I want to limit number of chars in input text field.



Code I'm using:



function limitText(field, maxChar){
if ($(field).val().length > maxChar){
$(field).val($(field).val().substr(0, maxChar));
}
}


Event is onkeyup.
When I type some text in input field cursor stays on the end of text but focus is backed on start of the text so I can't see cursor.



What can be a problem.



Browser is FF, on IE and chrome it is working correctly


More From » jquery

 Answers
1

you can also do it like this:



<input type=text name=usrname maxlength=10 />


to achieve this with jQuery, you can do this:



function limitText(field, maxChar){
$(field).attr('maxlength',maxChar);
}

[#83098] Wednesday, September 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rebekahalysah

Total Points: 304
Total Questions: 96
Total Answers: 102

Location: Taiwan
Member since Mon, May 2, 2022
2 Years ago
;