Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  20] [ 1]  / answers: 1 / hits: 96991  / 13 Years ago, tue, december 20, 2011, 12:00:00

I've seen some similar questions to mine here, but they don't really answer me...



So I'm doing this: (Inside the document ready function)



$(#dest).focusin(function() {
$(#dest).val($.trim($(#dest).val()));
});


The ideia is when the user focus on a input called #dest trim all the space characters on it (previously added using focusout for visual comfort).



Right now, nothing is happening. :(



Hope someone can help me a bit here.



Thanks!






Is this a computer related problem?
I've tested all the code provided by commenters and none works. I'm using Firefox and Safari under OSX (Snow Leopard) 10.6.8 and also Safari under 10.8.2 (Lion) and I got the same results... OSX problem? -- Everything is ok, check my last edit!






Final Edit and Solution thanks to Phil Klein



My problem was using incorrectly jQuery's trim() function... According to the trim() documentation it does the following:




The $.trim() function removes all newlines, spaces (including
non-breaking spaces), and tabs from the beginning and end of the
supplied string. If these whitespace characters occur in the middle of
the string, they are preserved.




Yesterday I didn't read the last part where it says from the beginning and end of the supplied string -- Sorry everyone. :(



Lucky and after the drawing above, @Phil Klein understood my mistake and helped me with a solution:



$(function() {
$(#dest).on(focus, function() {
var dest = $(this);
dest.val(dest.val().split( ).join());
});
});


You can read more about the solution and see an example here.



Thanks to @Phil Klein and also everyone who helped me on this one ;)


More From » jquery

 Answers
60

The example below removes all spaces from the contents of the textbox on focus. This particular example requires jQuery 1.7+ since it uses the new .on() API:



$(function() {
$(#dest).on(focus, function() {
var dest = $(this);
dest.val(dest.val().split( ).join());
});
});


See this example: http://jsfiddle.net/RnZ5Y/5/


[#88475] Sunday, December 18, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zoey

Total Points: 120
Total Questions: 103
Total Answers: 105

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
;