Difference between revisions of "DOM Shopping"

From ProgZoo
Jump to navigation Jump to search
Line 45: Line 45:
<pre class=usr>
<pre class=usr>
document.body.innerHTML = `
document.body.innerHTML = `
<div>Adult: Quantity: <select id=quantity value='1'>
<div>Adult: Quantity: <select id=adultvalue='1'>
               <option>1</option>
               <option>1</option>
               <option>2</option>
               <option>2</option>
               </select>
               </select>
</div>
</div>
<div>Child: Quantity: <select id=quantity value='1'>
<div>Child: Quantity: <select id=child value='1'>
               <option>1</option>
               <option>1</option>
               <option>2</option>
               <option>2</option>
Line 63: Line 63:
<pre class=ans>
<pre class=ans>
document.body.innerHTML = `
document.body.innerHTML = `
<div>Adult: Quantity: <select id=quantity value='1'>
<div>Adult: Quantity: <select id=adult value='1'>
               <option>1</option>
               <option>1</option>
               <option>2</option>
               <option>2</option>
               </select>
               </select>
</div>
</div>
<div>Child: Quantity: <select id=quantity value='1'>
<div>Child: Quantity: <select id=child value='1'>
               <option>1</option>
               <option>1</option>
               <option>2</option>
               <option>2</option>

Revision as of 18:38, 10 October 2021


Multiply Quantity by Price

  • You can quantity
  • Multiply the number by the price to get total cost
{expr:[{id:'total',prop:"value"}],
 actions:[{id:'recalculate',method:"click"},{id:'quantity',value:"2"},{id:'recalculate',method:"click"},]}
document.body.innerHTML = `
<div>Price: <input id=price disabled value='50'></div>
<div>Quantity: <input id=quantity value='1'></div>
<div><button id=recalculate>Recalculate</button></div>
<div>Total: <input id=total value='50' disabled></div>
`;
document.body.innerHTML = `
<div>Price: <input id=price disabled value='50'></div>
<div>Quantity: <input id=quantity value='1'></div>
<div><button id=recalculate>Recalculate</button></div>
<div>Total: <input id=total value='50' disabled></div>
`;
document.getElementById('recalculate').onclick = ()=>{
  document.getElementById('total').value =
    parseInt(document.getElementById('price').value) *
    parseInt(document.getElementById('quantity').value);
}


Sum Quantity by Price

  • Adult ticket costs £10
  • Child tickets costs £5
{expr:[{id:'total',prop:"value"}],
 actions:[{id:'adult',method:"change"},
          {id:'adult',value:"2"},
          {id:'adult',method:"change"},]}
document.body.innerHTML = `
<div>Adult: Quantity: <select id=adultvalue='1'>
               <option>1</option>
               <option>2</option>
               </select>
</div>
<div>Child: Quantity: <select id=child value='1'>
               <option>1</option>
               <option>2</option>
               </select>
</div>
<div>Total: <input id=total value='15' disabled></div>
`;
document.querySelectorAll('select').forEach(()=>{
})
document.body.innerHTML = `
<div>Adult: Quantity: <select id=adult value='1'>
               <option>1</option>
               <option>2</option>
               </select>
</div>
<div>Child: Quantity: <select id=child value='1'>
               <option>1</option>
               <option>2</option>
               </select>
</div>
<div>Total: <input id=total value='15' disabled></div>
`;
document.querySelectorAll('select').forEach(()=>{
  document.getElementById('total').value =
    parseInt(document.getElementById('adult').value) * 10 +
    parseInt(document.getElementById('child').value) * 5;
})