Link to home
Start Free TrialLog in
Avatar of Refael
RefaelFlag for United States of America

asked on

jQuery trim input val

Hello Experts,

Can someone help me understand why this if statement is not working?
Can i use the jQuery "trim" in this way on form input filed?

$(requiredInput).blur(function() {
  if (($.trim($(this).val() === "")) || ($.trim($(this).val().length <= 5))) {


Open in new window

Avatar of haloexpertsexchange
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Refael


Perfect, Thank you haloexpertsexchange
Can you explain what this it mean a string here?  .val() )
.val() gets you the value from which ever element you are currently accessing with $(this).val();
This is not to be confused with the text of an element.
Inputs have both and do not necessarily have the same information in both or even have both filled in.
Avatar of Refael


Hi haloexpertsexchange, thank you again.

So you basically it means that when i use "trim" i need to refer to the "text" (string) and not to the "value". by having this "val() )" . am i right?
Avatar of Rob
no - you need to use trim on a string.  you can also use it on val() / text() as they both return strings but you can't use it on a boolean such as true / false, which you were doing with your comparison:

...val() === "" <======== this returns a Boolean not a string
Avatar of Refael


Thank you tagit :-) now i understand.