tomhoppe.com

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

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;"

One Response to “Assign the child’s href to the parent’s onclick”

  1. Anonymous says:

    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.

Leave a Reply