Pontus
Pontus

Reputation: 61

How to put my fontawesome icon in my JavaScript button?

I built a button with JavaScript, and now I want it to be a shopping cart from Fontawesome in the button as well. I tried something like this:

var x = document.getElementByCLass("I").innerHTML

This is my button:

var btn = document.createElement("BUTTON");
var t = document.createTextNode("Lägg till i kundkorgen");

This is the icon I'm trying to call on:

<li>
  <a href="kundkorg.html">
    <i class="fa fa-shopping-cart fa-3x" aria-hidden="true"></i>
  </a>
</li>

It's not working and I'm stuck. I would love some pointers.

Upvotes: 2

Views: 4298

Answers (2)

BigBlast
BigBlast

Reputation: 387

Well, you must inject the entire tag to your recently created tag. You can achieve that in a lot of ways and one would be:

var btn = document.createElement("BUTTON");
btn.innerHtml = '<i class="fa fa-shopping-cart fa-3x" aria-hidden="true"></i> Lägg till i kundkorgen';

It should work! The rest of the code stills the same (assuming it is already working and your problem is only put the icon inside your button via Javascript). But i do recommend you not to use pure javascript to do that (i'm assuming you're not currently in a exclusive learning task, but in a job task) and instead try to learn jQuery or some related framework. It will help you do such thing with A LOT less code!

Upvotes: 2

user7222571
user7222571

Reputation:

this works

<button>
    <i class="fa fa-shopping-cart fa-3x"></i>
</button>

Upvotes: 0

Related Questions