📝 Edit page
➕ Add page
Basic
Selection
document.getElementById('myid');
document.getElementsByClassName('my-class');
document.getElementsByName('my-name');
document.getElementsByTagName('div');
document.querySelector('div.my-class');
document.querySelectorAll('div.my-class');
Edit
const el = document.getElementById('my-id');
el.getAttribute('src',);
el.setAttribute('src', 'https://example.com');
I think there is a cleaner way to do this with remove and no check, or toggle class.
// classList is now widely supported https://caniuse.com/#search=classList
if (el.classList.contains('my-class')) {
el.classList.remove('my-class');
}
See video.
el.innerHTML = "<b>Hello</b>"
// Safe for security issues.
el.innerContent = "<b>Hello</b>"
el.innerText = "Hello"
Create list
<select id='my-id'></select>
const selectInput = document.getElementById('my-id');
for (let value of ['a', 'b', 'c']) {
const option = document.createElement('option');
option.setAttribute('value', value);
option.text = value;
selectInput.appendChild(option);
}