DOM Controls that do things

From ProgZoo
Revision as of 19:50, 4 September 2021 by Andr3w (talk | contribs) (→‎Change Color)
Jump to navigation Jump to search

A button

When the user clicks the button the DOM gets changed.

let b = document.createElement('button');
b.innerText = 'click me';
b.onclick = ()=>{
  document.body.append('Hello world');
}
document.body.append(b);
let b = document.createElement('button');
b.innerText = 'click me';
b.onclick = ()=>{
  document.body.append('Hello World!');
}
document.body.append(b);

An input box and a button

The user can change the text that gets echoed.

let i = document.createElement('input');
i.value = 'New York ';
let b = document.createElement('button');
b.innerText = 'click me';
b.onclick = ()=>{
  document.body.append(i.value);
}
document.body.append(i,b);
let i = document.createElement('input');
i.value = 'New York ';
let b = document.createElement('button');
b.innerText = 'click me';
b.onclick = ()=>{
  document.body.append(i.value);
  document.body.append(i.value);
}
document.body.append(i,b);

Change Color

An input element can be a text box.

  • In this example the text box is created and given a default value
  • The user can change that value.
  • The button text should get changed to the color of the moment. You can do that with the line:
 b.style.color = i.value;
let i = document.createElement('input');
i.value = 'green';
let b = document.createElement('button');
b.innerText = 'click me';
b.onclick = ()=>{
}
document.body.append(i,b);
let i = document.createElement('input');
i.value = 'green';
let b = document.createElement('button');
b.innerText = 'click me';
b.onclick = ()=>{
  b.style.color = i.value;
}
document.body.append(i,b);