Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
195
rated 0 times [  200] [ 5]  / answers: 1 / hits: 15307  / 12 Years ago, fri, june 1, 2012, 12:00:00

I'm trying to make an input field which automatically puts a questionmark at the end of the typed text while typing.



I just came up with this code but obviously it generates multiple questionmarks.



$(#id).keyup(function(){
$(this).val($(this).val() + ?);
});


Thank you for your ideas.


More From » jquery

 Answers
34
$(#id).keyup(function(){
if ($(this).val().split('').pop() !== '?') {
$(this).val($(this).val() + ?);
}
});


DEMO



EDIT:



(function($) {
$.fn.setCursorPosition = function(pos) {
if ($(this).get(0).setSelectionRange) {
$(this).get(0).setSelectionRange(pos, pos);
} else if ($(this).get(0).createTextRange) {
var range = $(this).get(0).createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
}(jQuery));
$(#id).keyup(function(){
if ($(this).val().split('').pop() !== '?') {
$(this).val($(this).val() + ?);
$(this).setCursorPosition( $(this).val().length - 1)
}
});​


new DEMO


[#85204] Thursday, May 31, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lidialyrick

Total Points: 737
Total Questions: 104
Total Answers: 89

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
lidialyrick questions
;