Strings vs. Fragments
While we can't query or run an event listener on HTML strings, we can do it to fragments. So how do we turn a string into a fragment?
HTML string:
function renderProduct(product) {
const html = `
<div class="products">
<h2>${priduct.name}</h2>
<img src="${product.image}" alt="${product.name}"/>
<button>Add $<span class="currency">${product.currency}</span> ${(product.price_cents /100).toFixed(2)}</button>
</div>`;
const fragment =
document.createRange().createContextualFragment(html); // this line takes the html string above and turns it into a fragment
const button = fragment.querySelector('button');
button.addEventListener('click',handleButtonClick);
return fragment;
};
Strings vs. Fragments While we can't query or run an event listener on HTML strings, we can do it to fragments. So how do we turn a string into a fragment? HTML string:
Fragment:
Yarn
Procedure:index.js
file:yarn add express
to add express to your projectnode index.js
yarn start
and the site should be deployed to http://localhost:3000control+c
Using Stripe https://stripe.com/docs/api
yarn add stripe