Difference between revisions of "DOM Common Logic"
Jump to navigation
Jump to search
(Created page with "<pre id='shellbody' data-qtp='clicky'></pre> ==A button== <div class=qu> You must be this tall to ride <pre class=usr> let l = document.createElement('label'); l.innerHTML = '...") |
|||
(33 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<pre id='shellbody' data-qtp='clicky'></pre> | <pre id='shellbody' data-qtp='clicky'></pre> | ||
==A button== | ==A checkbox to enable a button== | ||
<div class=qu> | <div class=qu data-width=300> | ||
You must be this tall to | You must be 1.2m to to ride | ||
*The submit button starts with the property '''disabled''' | |||
*The users action removes this property | |||
*Add this code to allow the user to enable the button. | |||
document.getElementById('tall').onclick = ()=>{ | |||
document.getElementById('ride').removeAttribute('disabled') | |||
} | |||
<pre class=test> | |||
{expr:[{id:'ride',prop:"disabled"}], | |||
actions:[{},{id:'tall',method:"click"}]} | |||
</pre> | |||
<pre class=usr> | |||
document.body.innerHTML = ` | |||
<label>I am over 1.2m | |||
<input type=checkbox id=tall> | |||
</label> | |||
<button id=ride disabled>Ride the rollercoaster</button> | |||
`; | |||
document.getElementById('ride').onclick = ()=>{ | |||
document.body.append('Whee...'); | |||
} | |||
</pre> | |||
<pre class=ans> | |||
document.body.innerHTML = ` | |||
<label>I am over 1.2m | |||
<input type=checkbox id=tall> | |||
</label> | |||
<button id=ride disabled>Ride the rollercoaster</button> | |||
`; | |||
document.getElementById('ride').onclick = ()=>{ | |||
document.body.append('Whee...'); | |||
} | |||
document.getElementById('tall').onclick = ()=>{ | |||
document.getElementById('ride').removeAttribute('disabled') | |||
} | |||
</pre> | |||
</div> | |||
==Age verification== | |||
<div class=qu data-width=300> | |||
You must be 18 to see the smutty image. | |||
<pre class=test> | |||
{expr:[{id:'view',prop:"disabled"}], | |||
actions:[{},{id:'adult',method:"click"}]} | |||
</pre> | |||
<pre class=usr> | <pre class=usr> | ||
document.body.innerHTML = ` | |||
<label>I am over 18 | |||
<input type=checkbox id=adult> | |||
</label> | |||
<button id=view disabled>View Image</button> | |||
<img id=image width=200> | |||
`; | |||
document.getElementById('view').onclick=()=>{ | |||
document.getElementById('image').src | |||
document. | = 'https://progzoo.net/images/smut.jpg'; | ||
} | |||
</pre> | |||
<pre class=ans> | |||
document.body.innerHTML = ` | |||
<label>I am over 18 | |||
<input type=checkbox id=adult> | |||
</label> | |||
<button id=view disabled>View Image</button> | |||
<img id=image width=200> | |||
`; | |||
document.getElementById('view').onclick=()=>{ | |||
document.getElementById('image').src | |||
= 'https://progzoo.net/images/smut.jpg'; | |||
} | |||
document.getElementById('adult').onclick = ()=>{ | |||
document.getElementById('view').removeAttribute('disabled'); | |||
} | } | ||
</pre> | </pre> | ||
</div> | |||
==Express Delivery== | |||
<div class=qu data-width=300> | |||
Pay £10 extra for express delivery | |||
*When the user selects the option, the price should change | |||
*If the user unchecks the box it should go back to the original price | |||
*You can read the state of the check box with: | |||
if (document.getElementById('express').checked) | |||
... | |||
else | |||
... | |||
<pre class=test> | |||
{expr:[{id:'total',prop:'value'}], | |||
actions:[{},{id:'express',method:"click"},{id:'express',method:"click"}]} | |||
</pre> | |||
<pre class=usr> | |||
document.body.innerHTML = ` | |||
<div>Cost £<input disabled size=2 id=cost value=100><div> | |||
<div> | |||
<label>Express delivery (£10) | |||
<input id=express type=checkbox /> | |||
</label> | |||
</div> | |||
<div>Total £<input disabled id=total size=2 value=100></div> | |||
`; | |||
</pre> | |||
<pre class=ans> | <pre class=ans> | ||
document.body.innerHTML = ` | |||
<div>Cost £<input disabled size=2 id=cost value=100><div> | |||
<div> | |||
document.body. | <label>Express delivery (£10) | ||
<input id=express type=checkbox /> | |||
</label> | |||
</div> | |||
<div>Total £<input disabled id=total size=2 value=100></div> | |||
`; | |||
document.getElementById('express').onclick=()=>{ | |||
if (document.getElementById('express').checked) | |||
document.getElementById('total').value = '110'; | |||
else | |||
document.getElementById('total').value = '100'; | |||
} | |||
</pre> | |||
</div> | |||
==Discount Code== | |||
<div class=qu data-width=300> | |||
If the user enters discount code <code>10PC</code> then the total is £90 | |||
<pre class=test> | |||
{expr:[{id:'total',prop:'value'}], | |||
actions:[{}, | |||
{id:'recalculate',method:'click'}, | |||
{id:'discount',prop:'value',value:'wrong'}, | |||
{id:'recalculate',method:'click'}, | |||
{id:'discount',prop:'value',value:'10PC'}, | |||
{id:'recalculate',method:'click'}, | |||
]} | |||
</pre> | |||
<pre class=usr> | |||
document.body.innerHTML = ` | |||
<div>Cost £<input disabled size=2 id=cost value=100><div> | |||
<div> | |||
<label>Discount code | |||
<input id=discount /> | |||
</label> | |||
</div> | |||
<div><button id=recalculate>Recalculate</button> | |||
Total £<input disabled id=total size=2 value=100> | |||
</div> | |||
`; | |||
</pre> | |||
<pre class=ans> | |||
document.body.innerHTML = ` | |||
<div>Cost £<input disabled size=2 id=cost value=100><div> | |||
<div> | |||
<label>Discount code | |||
<input id=discount /> | |||
</label> | |||
</div> | |||
<div><button id=recalculate>Recalculate</button> | |||
Total £<input disabled id=total size=2 value=100> | |||
</div> | |||
`; | |||
document.getElementById('recalculate').onclick = ()=>{ | |||
if (document.getElementById('discount').value === '10PC'){ | |||
document.getElementById('total').value = '90'; | |||
}else{ | |||
document.getElementById('total').value = '100'; | |||
} | |||
} | |||
</pre> | |||
</div> | |||
==Exclusive Options== | |||
<div class=qu data-width=300> | |||
*You can use radio buttons to present mutually exclusive options | |||
<pre class=test> | |||
{expr:[{id:'total',prop:'value'}], | |||
actions:[ | |||
{id:'s',method:'click'}, | |||
{id:'d',method:'click'}, | |||
{id:'l',method:'click'}, | |||
]} | |||
</pre> | |||
<pre class=usr> | |||
document.body.innerHTML = ` | |||
<label>Single, £100 <input name=n type=radio id=s></label><br> | |||
<label>Double, £150 <input name=n type=radio id=d></label><br> | |||
<label>Luxury, £200 <input name=n type=radio id=l></label><br> | |||
Total £<input disabled id=total size=2> | |||
`; | |||
document.getElementById('s').onclick=()=>{ | |||
document.getElementById('total').value=100; | |||
} | |||
</pre> | |||
<pre class=ans> | |||
document.body.innerHTML = ` | |||
<label>Single, £100 <input name=n type=radio id=s></label><br> | |||
<label>Double, £150 <input name=n type=radio id=d></label><br> | |||
<label>Luxury, £200 <input name=n type=radio id=l></label><br> | |||
Total £<input disabled id=total size=2> | |||
`; | |||
document.getElementById('s').onclick=()=>{ | |||
document.getElementById('total').value=100; | |||
} | |||
document.getElementById('d').onclick=()=>{ | |||
document.getElementById('total').value=150; | |||
} | |||
document.getElementById('l').onclick=()=>{ | |||
document.getElementById('total').value=200; | |||
} | } | ||
</pre> | </pre> | ||
</div> | </div> |
Latest revision as of 15:30, 10 October 2021
A checkbox to enable a button
You must be 1.2m to to ride
- The submit button starts with the property disabled
- The users action removes this property
- Add this code to allow the user to enable the button.
document.getElementById('tall').onclick = ()=>{ document.getElementById('ride').removeAttribute('disabled') }
{expr:[{id:'ride',prop:"disabled"}], actions:[{},{id:'tall',method:"click"}]}
document.body.innerHTML = ` <label>I am over 1.2m <input type=checkbox id=tall> </label> <button id=ride disabled>Ride the rollercoaster</button> `; document.getElementById('ride').onclick = ()=>{ document.body.append('Whee...'); }
document.body.innerHTML = ` <label>I am over 1.2m <input type=checkbox id=tall> </label> <button id=ride disabled>Ride the rollercoaster</button> `; document.getElementById('ride').onclick = ()=>{ document.body.append('Whee...'); } document.getElementById('tall').onclick = ()=>{ document.getElementById('ride').removeAttribute('disabled') }
Age verification
You must be 18 to see the smutty image.
{expr:[{id:'view',prop:"disabled"}], actions:[{},{id:'adult',method:"click"}]}
document.body.innerHTML = ` <label>I am over 18 <input type=checkbox id=adult> </label> <button id=view disabled>View Image</button> <img id=image width=200> `; document.getElementById('view').onclick=()=>{ document.getElementById('image').src = 'https://progzoo.net/images/smut.jpg'; }
document.body.innerHTML = ` <label>I am over 18 <input type=checkbox id=adult> </label> <button id=view disabled>View Image</button> <img id=image width=200> `; document.getElementById('view').onclick=()=>{ document.getElementById('image').src = 'https://progzoo.net/images/smut.jpg'; } document.getElementById('adult').onclick = ()=>{ document.getElementById('view').removeAttribute('disabled'); }
Express Delivery
Pay £10 extra for express delivery
- When the user selects the option, the price should change
- If the user unchecks the box it should go back to the original price
- You can read the state of the check box with:
if (document.getElementById('express').checked) ... else ...
{expr:[{id:'total',prop:'value'}], actions:[{},{id:'express',method:"click"},{id:'express',method:"click"}]}
document.body.innerHTML = ` <div>Cost £<input disabled size=2 id=cost value=100><div> <div> <label>Express delivery (£10) <input id=express type=checkbox /> </label> </div> <div>Total £<input disabled id=total size=2 value=100></div> `;
document.body.innerHTML = ` <div>Cost £<input disabled size=2 id=cost value=100><div> <div> <label>Express delivery (£10) <input id=express type=checkbox /> </label> </div> <div>Total £<input disabled id=total size=2 value=100></div> `; document.getElementById('express').onclick=()=>{ if (document.getElementById('express').checked) document.getElementById('total').value = '110'; else document.getElementById('total').value = '100'; }
Discount Code
If the user enters discount code 10PC
then the total is £90
{expr:[{id:'total',prop:'value'}], actions:[{}, {id:'recalculate',method:'click'}, {id:'discount',prop:'value',value:'wrong'}, {id:'recalculate',method:'click'}, {id:'discount',prop:'value',value:'10PC'}, {id:'recalculate',method:'click'}, ]}
document.body.innerHTML = ` <div>Cost £<input disabled size=2 id=cost value=100><div> <div> <label>Discount code <input id=discount /> </label> </div> <div><button id=recalculate>Recalculate</button> Total £<input disabled id=total size=2 value=100> </div> `;
document.body.innerHTML = ` <div>Cost £<input disabled size=2 id=cost value=100><div> <div> <label>Discount code <input id=discount /> </label> </div> <div><button id=recalculate>Recalculate</button> Total £<input disabled id=total size=2 value=100> </div> `; document.getElementById('recalculate').onclick = ()=>{ if (document.getElementById('discount').value === '10PC'){ document.getElementById('total').value = '90'; }else{ document.getElementById('total').value = '100'; } }
Exclusive Options
- You can use radio buttons to present mutually exclusive options
{expr:[{id:'total',prop:'value'}], actions:[ {id:'s',method:'click'}, {id:'d',method:'click'}, {id:'l',method:'click'}, ]}
document.body.innerHTML = ` <label>Single, £100 <input name=n type=radio id=s></label><br> <label>Double, £150 <input name=n type=radio id=d></label><br> <label>Luxury, £200 <input name=n type=radio id=l></label><br> Total £<input disabled id=total size=2> `; document.getElementById('s').onclick=()=>{ document.getElementById('total').value=100; }
document.body.innerHTML = ` <label>Single, £100 <input name=n type=radio id=s></label><br> <label>Double, £150 <input name=n type=radio id=d></label><br> <label>Luxury, £200 <input name=n type=radio id=l></label><br> Total £<input disabled id=total size=2> `; document.getElementById('s').onclick=()=>{ document.getElementById('total').value=100; } document.getElementById('d').onclick=()=>{ document.getElementById('total').value=150; } document.getElementById('l').onclick=()=>{ document.getElementById('total').value=200; }