Mike Barwick
Mike Barwick

Reputation: 5367

$.trim spaces at beginning and end of text input value

So I'm trying to trim any spaces BEFORE and AFTER the text input value...

Any ideas? Driving me nuts. I tried reading up on $.trim(). But can't get it to work properly.

Code:

var campaign_values = {
        name : $.trim($('.nameField').val().toLowerCase().replace(/\s/g, "_")),
        // other variables...
};

Upvotes: 2

Views: 9845

Answers (3)

Mike Brant
Mike Brant

Reputation: 71422

Not sure what problem you were having with trim, but you should be able to use it like this:

var campaign_values = {
        name : $.trim($('.nameField').val()).toLowerCase().replace(/\s/g, "_"),
        // other variables...
};

Though in this case, I think it is clearer to use javascript trim()

var campaign_values = {
        name : $('.nameField').val().trim().toLowerCase().replace(/\s/g, "_"),
        // other variables...
};

Upvotes: 8

Ram
Ram

Reputation: 144729

Try using $.trim before calling replace function:

name : $.trim($('.nameField').val()).toLowerCase().replace(/\s/g, "_"),

Upvotes: 4

Hacknightly
Hacknightly

Reputation: 5164

The standard JavaScript String.trim() will remove the beginning and end whitespace from a string.

"   string  ".trim() ==  "string"

Upvotes: 0

Related Questions