Ryan Sayles
Ryan Sayles

Reputation: 3431

Button in div tag

I have a div footer and I want to place links to various social media sites in it. Currently I have:

<div id="footer">
    <button type="button"
       onclick="href='window.location.https://www.facebook.com'">
       <img class="socimage" src="Facebook_icon.jpg"/></button>
</div>

I'm using a regular button tag because off hand that was the easiest way I can think of to also add an image to it and have it set to a css. The problem is the link is not working. I'm sure style wise there is a better way, maybe using forms but I'm a little rusty on html. Also as I stated I want more than one button so if I should use a form tag I don't want the link within that tag

Upvotes: 1

Views: 18137

Answers (3)

gustavohenke
gustavohenke

Reputation: 41440

You're using it wrong -- the correct way would be:

<button type="button" onclick="location = 'https://www.facebook.com'"><img class="socimage" src="Facebook_icon.jpg"/></button>

However, it would be better to just use an <a> tag, just like this:

<a href="https://facebook.com"><img class="socimage" src="Facebook_icon.jpg"></a>

Upvotes: 2

clinton3141
clinton3141

Reputation: 4841

The best solution to this is to use an a tag rather than button

<div id="footer">
  <a href="https://www.facebook.com/"><img class="socimage" src="Facebook_icon.jpg" /></a>
</div>

If you need to keep the button implementation for some reason, there is an error in your onclick code. It should be:

onclick='location.href="https://www.facebook.com/"'

Upvotes: 0

Quentin
Quentin

Reputation: 943185

I'm using a regular button tag because off hand that was the easiest way I can think of to also add an image to it and have it set to a css.

It isn't. The easiest way is to use a regular link:

<a href="https://www.facebook.com"><img class="socimage" src="Facebook_icon.jpg"></a>

The problem is the link is not working

Your JavaScript is a big syntax error. If you want to use JS to redirect the browser, then you need to assign a string to location:

location = "https://www.facebook.com/";

Upvotes: 2

Related Questions