Two Javascript Tricks: click() and trim()

Man my poor blog hasn’t been updated in quite some time. What on earth could I do to remedy that situation? Wait, I GOT IT! I’ll add a new post! 🙂

Here are two really simple but really useful JavaScript tricks.

#1

document.getElementById(x).click();

The above is handy when you want to programmatically click a button for the user based on whatever logic you write. I used it to fire some ATLAS (now called ASP.Net AJAX) animations after I validated some fields.

#2

Add the following to your JavaScript library to trim the white space from the beginning and end of your strings.

String.prototype.trim=function()
{
	return this.replace(/^\s*|\s*$/g,'');
}

The above allows you to do this: xString = xString.trim();

Check out this Wikipedia article on ‘trim’. There are examples on how to trim a string for numerous programming languages.

http://en.wikipedia.org/wiki/Trim_(programming)

2 Replies to “Two Javascript Tricks: click() and trim()”

Leave a Reply

Your email address will not be published. Required fields are marked *