heron
heron

Reputation: 3661

Trim extra space with javascript

I'm using PHP tidy and it automatically indents markup, adds tabs and spaces. Please take a look at this.

http://jsfiddle.net/njh85/

I got 2 questions:

enter image description here

Upvotes: 0

Views: 1258

Answers (5)

vsync
vsync

Reputation: 130371

As of JavaScript 1.8.1 (ES5) you can use normal string.trim()

var orig = "   foo  ";
alert( orig.trim() );

// "foo"

Upvotes: 1

pimvdb
pimvdb

Reputation: 154888

You're getting spaces because your options' text is on a separate line in HTML with indenting. You could remove them first:

$("#users option").text(function(i, text) {
    return $.trim(text);
});

As for the placeholder, use .attr("placeholder", ...) instead of .val(...):

input = $( "<input>" )
    .appendTo( wrapper )
    .attr( "placeholder", value )

http://jsfiddle.net/njh85/6/

Upvotes: 2

benbai123
benbai123

Reputation: 1463

Trim left and right spaces of a string

string.replace(/^\s\s*/, '').replace(/\s\s*$/, '');

Regarding to placeholder, you can try jQuery-Placeholder

Upvotes: 0

adeneo
adeneo

Reputation: 318282

How about using $.trim :

FIDDLE

Upvotes: 2

TheHe
TheHe

Reputation: 2972

take a look @ http://api.jquery.com/jQuery.trim/

Upvotes: 0

Related Questions