This is your friend when you need to trim whitespace in javascript:

replace(/^\s*|\s*$/g,”)