rbi-learning / Today-I-Learned

1 stars 0 forks source link

Today I Learned 8/17 #148

Open paulapereira1 opened 4 years ago

paulapereira1 commented 4 years ago

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) {
  return`
  <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>`;
};

Fragment:

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

Yarn Procedure:

Using Stripe https://stripe.com/docs/api