recursion.ninja
recursion.ninja

Reputation: 5488

What is a more elegant way to trim data with jQuery

I have the following jQuery code:

$(myObj).val( $.trim($(myObj).val()) );

From my experience with jQuery and it's elegant approach to common scripting tasks I feel like jQuery should provide a more elegant way to trim an objects value data. Is there a more elegant way in which the object is not called twice?

Upvotes: 2

Views: 297

Answers (2)

John Vinyard
John Vinyard

Reputation: 13495

Assign it to a variable.

var a = $(myObj);
a.val($.trim(a.val()));

Upvotes: 0

Kyle
Kyle

Reputation: 22268

$(myObj).val(function(i, val){
  return $.trim(val);
});

See this example from the jQuery val docs.

Upvotes: 4

Related Questions