DOM Common Logic

From ProgZoo
Revision as of 14:29, 3 October 2021 by Andr3w (talk | contribs) (→‎A button)
Jump to navigation Jump to search

A checkbox to enable a button

You must be 1.2m to to ride

  • Set the type of input c to "checkbox"
  • Add the onclick function to c
let l = document.createElement('label');
l.innerHTML = 'Confirm I am at least 1.2m';
let c = document.createElement('input');
l.append(c);
c.id = 'tall';
let b = document.createElement('button');
b.innerText = 'Ride the Rollercoaster';
b.disabled = true;
b.onclick = ()=>{
  document.body.append('Weeee...');
}
document.body.append(l,b);
let l = document.createElement('label');
l.innerHTML = 'Confirm I am at least 1.2m';
let c = document.createElement('input');
c.type = 'checkbox';
l.append(c);
c.id = 'tall';
let b = document.createElement('button');
b.innerText = 'Ride the Rollercoaster';
b.disabled = true;
b.onclick = ()=>{
  document.body.append('Weeee...');
}
c.onclick = ()=>{
  if (c.checked){
    b.removeAttribute('disabled');
  }else{
    b.addAttribute('disabled');
  }
}
document.body.append(l,b);