72

Everything else in my site seems to be compatible with all browsers except for my links. They appear on the page, but they do not work. My code for the links are as follows-

<td bgcolor="#ffffff" height="370" valign="top" width="165">
    <p>
        <a href="sc3.html">
            <button style="width:120;height:25">Super Chem #3</button>
        </a> 
        <a href="91hollywood.html">
            <button style="width:120;height:25">91 Hollywood</button>
        </a> 
        <a href="sbubba.html">
            <button style="width:120;height:25">Super Bubba</button>
        </a> 
        <a href="afgoohash.html">
            <button style="width:120;height:25">Afgoo Hash</button>
        </a> 
        <a href="superjack.html">
            <button style="width:120;height:25">Super Jack</button>
        </a> 
        <a href="sog.html">
            <button style="width:120;height:25">Sugar OG</button>
        </a> 
        <a href="91pk91.html">
            <button style="width:120;height:25">91 x PK</button>
        </a> 
        <a href="jedi1.html">
            <button style="width:120;height:25">Jedi</button>
        </a>
    </p>
    <p>&nbsp;</p>
    <a href="http://indynile99.blogspot.com">
        <button style="width:120;height:25">Blog</button>
    </a>
    <p>&nbsp;</p>
</td>
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
  • Possible duplicate of [Link inside a button not working in Firefox](http://stackoverflow.com/questions/17253410/link-inside-a-button-not-working-in-firefox) – 2540625 Jan 24 '17 at 19:38

13 Answers13

87

You can't have a <button> inside an <a> element. As W3's content model description for the <a> element states:

"there must be no interactive content descendant."

(a <button> is considered interactive content)

To get the effect you're looking for, you can ditch the <a> tags and add a simple event handler to each button which navigates the browser to the desired location, e.g.

<input type="button" value="stackoverflow.com" onClick="javascript:location.href = 'http://stackoverflow.com';" />

Please consider not doing this, however; there's a reason regular links work as they do:

  • Users can instantly recognize links and understand that they navigate to other pages
  • Search engines can identify them as links and follow them
  • Screen readers can identify them as links and advise their users appropriately

You also add a completely unnecessary requirement to have JavaScript enabled just to perform a basic navigation; this is such a fundamental aspect of the web that I would consider such a dependency as unacceptable.

You can style your links, if desired, using a background image or background color, border and other techniques, so that they look like buttons, but under the covers, they should be ordinary links.

Christian Jensen
  • 900
  • 1
  • 10
  • 25
Rob
  • 47,999
  • 5
  • 74
  • 91
19

Just a note:
W3C has no problem with button inside of link tag, so it is just another MS sub-standard.

Answer:
Use surrogate button, unless you want to go for a full image.

Surrogate button can be put into tag (safer, if you use spans, not divs).

It can be styled to look like button, or anything else.

It is versatile - one piece of css code powers all instances - just define CSS once and from that point just copy and paste html instance wherever your code requires it.

Every button can have its own label - great for multi-lingual pages (easier that doing pictures for every language - I think) - also allows to propagate instances all over your script easier.

Adjusts its width to label length - also takes fixed width if it is how you want it.
IE7 is an exception to above - it must have width, or will make this button from edge to edge - alternatively to giving it width, you can float button left
- css for IE7:
a. .width:150px; (make note of dot before property, I usually target IE7 by adding such dot - remove dot and property will be read by all browsers)
b. text-align:center; - if you have fixed width, you have to have this to center text/label
c. cursor:pointer; - all IE must have this to show link pointer correctly - good browsers do not need it

You can go step forward with this code and use CSS3 to style it, instead of using images:
a. radius for round corners (limitation: IE will show them square)
b. gradient to make it "button like" (limitation: opera does not support gradients, so remember to set standard background colour for this browser)
c. use :hover pclass to change button states depending on mouse pointer position etc. - you can apply it to text label only, or whole button

CSS code below:

.button_surrogate span { margin:0; display:block; height:25px; text-align:center; cursor:pointer; .width:150px; background:url(left_button_edge.png) left top no-repeat; }

.button_surrogate span span { display:block; padding:0 14px; height:25px; background:url(right_button_edge.png) right top no-repeat; }

.button_surrogate span span span { display:block; overflow:hidden; padding:5px 0 0 0; background:url(button_connector.png) left top repeat-x; }

HTML code below (button instance):

<a href="#">
  <span class="button_surrogate">
     <span><span><span>YOUR_BUTTON_LABEL</span></span></span>
  </span>
</a>
Mike
  • 23,542
  • 14
  • 76
  • 87
Jeffz
  • 2,075
  • 5
  • 36
  • 51
15
$("a > button").live('click', function() { 
    location.href = $(this).closest("a").attr("href");
}); // fixed
iisisrael
  • 583
  • 6
  • 6
Kalmár Gábor
  • 434
  • 4
  • 10
  • 8
    For those that are wondering, this appears to be a bit of code that uses out of date jQuery functions. With current versions of jQuery you'll need to replace .live with .on. If you're using prototypejs instead, try starting with the $$ function. – Eric Feb 04 '13 at 21:14
  • A button inside a link is not valid HTML and has undefined behavior. – Odin Dec 22 '13 at 22:10
  • I'm having problems with this in IE8 – Dusty Feb 11 '14 at 16:21
  • What is the version of jQuery you use? What is the exact problem? – Kalmár Gábor Feb 12 '14 at 13:06
13

The code below will work just fine in all browsers:

<button onClick="location.href = 'http://www.google.com'">Go to Google</button>
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Chris H.
  • 147
  • 1
  • 3
  • 2
    This is not very good for search engine optimization, though. Not recommended if you want the page it links to get high ranking. – pkout Apr 12 '13 at 05:43
12

You cannot have a button inside an a tag. You can do some javascript to make it work however.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 1
    Whats funny, I tried this like 12 years ago when I was learning HTML. Heh. – Daniel A. White Apr 29 '09 at 15:28
  • 20
    Does the W3C dictate that you can't have a button in an anchor or is that just a Microsoft implementation? – James McMahon Dec 16 '09 at 17:54
  • 2
    Bad Microsoft implementation, also generally bad practice. There is nothing in the specification dictating buttons can't go inside links, but bad practice because you have 2 competing events which need to happen when a user clicks it - does it press the button or follow the link? It is this that confuses IE (along with a great many other things!) and a very good reason not to do it. – Ben Green Jan 20 '14 at 08:15
  • It's bad MSFT implementation. The spec for HTML5 states that the anchor tag can act as a block element. – dudewad Jun 30 '14 at 23:13
9

If you're using a framework like Twitter bootstrap you could simply add class "btn" to an a tag -- I just had a user call in about this problem apparently my checkout button wasn't working because I had a button inside an a tag... instead I just added the btn class to it and it works perfectly now.. Big mistake, one that probably cost customers -- and I sometimes forget to code check everything works in IE..

E.g. <a href="link.com" class="btn" >Checkout</a>

PatrickCurl
  • 996
  • 1
  • 7
  • 8
6

i found that this works for me

<input type="button" value="click me" onclick="window.open('http://someurl', 'targetname');">
lance
  • 61
  • 1
  • 1
6

Since this is only an issue in IE, to resolve this, I'm first detecting if the browser is IE and if so, use a jquery click event. I put this code into a global file so it's fixed throughout the site.

if (navigator.appName === 'Microsoft Internet Explorer')
{
    $('a input.button').click(function()
    {
        window.location = $(this).closest('a').attr('href');
    });
}

This way we only have the overhead of assigning click events to linked input buttons for IE. note: The above code is inside a document ready function to make sure the page is completely loaded before assigning click events.

I also assigned class names to my input buttons to save some overhead when using IE so I can search for

$('a input.button')

instead of

$('a input[type=button]')

.

<a href="some_link"><input type="button" class="button" value="some value" /></a>

On top of all that, I'm also utilizing CSS to make the buttons look clickable when your mouse hovers over it:

input.button {cursor: pointer}
Francis Lewis
  • 8,872
  • 9
  • 55
  • 65
2

<form:form method="GET" action="home.do"> <input id="Back" class="sub_but" type="submit" value="Back" /> </form:form>

This is works just fine I had tested it on IE9.

  • Does this answer the question? `Button inside of anchor link works in Firefox but not in Internet Explorer?` – Ajeeb.K.P Sep 04 '15 at 09:26
1

The problem here is that the link sits behind the button, even when changing the z-index. This markup is also invalid (read the spec). So the reason the links dont work is because you are actually clicking the button and not the link. The solution is to change your markup around.

<button type="button"><a href="yourlink">Link</a></button>

Then style as needed. A demo is here.

lukeocom
  • 3,243
  • 3
  • 18
  • 30
  • this seemingly only works in Chrome, not in Firefox or IE – Jeff Puckett May 27 '16 at 15:19
  • specifically from the [html5 button spec](https://www.w3.org/TR/html5/forms.html#the-button-element): "there must be no [interactive content](https://www.w3.org/TR/html5/dom.html#interactive-content-0) descendant." which lists the anchor tag as the first example. – Jeff Puckett May 27 '16 at 15:59
1

This is very simple actually, no javascript required.

First, put the anchor inside the button

<button><a href="#">Bar</a></button>

Then, turn the anchor tag into a block type element and fill its container

button a {
  display:block;
  height:100%;
  width:100%;
}

Finally, add other styles as necessary.

Alex Hadley
  • 2,125
  • 2
  • 28
  • 50
Ryan Tuosto
  • 1,941
  • 15
  • 23
  • this seemingly only works in Chrome, not in Firefox or IE – Jeff Puckett May 27 '16 at 15:20
  • specifically from the [html5 button spec](https://www.w3.org/TR/html5/forms.html#the-button-element): "there must be no [interactive content](https://www.w3.org/TR/html5/dom.html#interactive-content-0) descendant." which lists the anchor tag as the first example. – Jeff Puckett May 27 '16 at 15:59
0

just insert this jquery code to your HTML's head section:

<!--[if lt IE 9 ]>
     <script>
        $(document).ready(function(){
            $('a > button').click(function(){
                window.location.href = $(this).parent().attr('href');
            });
        });
    </script>
<![endif]-->
user1447420
  • 1,382
  • 2
  • 12
  • 14
0

Why not just convert all <button> to <span> with type="button" and then style with your normal css button classes? Just confirmed that this works in IE11.

calyxofheld
  • 1,538
  • 3
  • 24
  • 62