tomhoppe.com

Racing, Web Development, Photography, and Beer...Stuff that matters.

Tuesday, July 17, 2007

Assign the child's href to the parent's onclick

This is a cool little snippet for making realistic CSS buttons. It will do an onclick location on the element to the href of its first a element.

Makes it nice when you have a "fake button" done through CSS and you want the whole thing to be clickable.

What I usually try to do is CSS the A tag display:block and assign it a width, height and padding, but when you can't do that, this little snippet is a life saver

onclick="if (this.getElementsByTagName('a')[0])
location=this.getElementsByTagName('a')[0].href;"

Labels: , ,

1 Comments:

Anonymous Anonymous said...

Tom,
Very useful! A little note, I had to do it this way: onclick="if (this.getElementsByTagName('a')[0])
{location=this.getElementsByTagName('a')[0].href};" in order for it to work. Thanks, for the help.

July 10, 2008 at 11:09 PM  

Post a Comment

<< Home