type="radio"
<input> element as "radio buttons"
JS.info ⟩ Form properties and methods ⭐️⭐️
Radio Button 事件切換實作 - jQuery
MDN ⟩
"change" event
const list = $('.ice-cream');
list.onchange = (e) => {
const result = $('.result');
result.textContent = `You like ${e.target.value}`;
};<label>Choose an ice cream flavor:
<select class="ice-cream" name="ice-cream">
<option value="">Select One …</option>
<option value="chocolate">Chocolate</option>
<option value="sardine">Sardine</option>
<option value="vanilla">Vanilla</option>
</select>
</label>
<div class="result"></div>code example
import { $, /* $all, */ log } from './layout/helpers.mjs';
const flexbox = $('.flex');
// append more flex items
const n = 7;
for (let i = 0; i < n; i++) {
let div = document.createElement('div');
flexbox.appendChild(div);
}
//
const flex = $('.flex');
// controls
const form = $('form');
// flex-direction
// --------------
// $('#reverse'): HTMLInputElement
// ⭐️ form.dir: RadioNodeList
// ⭐️ $all('.dir > input[type="radio"]'): NodeList
// dirction on change
$('.dir').onchange = (e) => {
// ⭐️ 注意:e.target 可能是 checkbox❗️
let dir = form.dir.value;
let postfix = form.reverse.checked ? '-reverse' : '';
flex.style.flexDirection = `${dir}${postfix}`;
};
// flex-wrap
// ----------
// ⭐️ form.wrap: RadioNodeList
// wrap on change
$('.wrap').onchange = (e) => {
flex.style.flexWrap = e.target.value;
};
Last updated
Was this helpful?