Two Javascript Tricks: click() and trim()

November 27, 2006
Categories: Web & Software Development
Tags:

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)

  1. 2 Responses to “Two Javascript Tricks: click() and trim()”

  2. By alison on Nov 27, 2006

    nice new intro. you are dusty reagan and you <3 computers!

  3. By Dusty Reagan on Nov 28, 2006

    FACT! Dusty <3s Computers! It’s as true as 1+1 = math. ;)

Post a Comment