예시1 체크박스
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript">
const checkForm = function () {
let chkArr = document.frm.cb.length;
for (let i = 0; i < chkArr; i++) {
if (document.frm.cb[i].checked) {
console.log(document.frm.cb[i].value);
}
}
}
</script>
</head>
<body>
<form name="frm">
<input type="checkbox" name="chk1" value="사과">사과<br />
<input type="checkbox" name="chk2" value="바나나">바나나<br />
<input type="checkbox" name="chk3" value="딸기">딸기<br />
<input type="checkbox" name="chk4" value="포도">포도<br />
<br />
<input type="checkbox" name="cb" value="사과">사과<br />
<input type="checkbox" name="cb" value="바나나">바나나<br />
<input type="checkbox" name="cb" value="딸기">딸기<br />
<input type="checkbox" name="cb" value="포도">포도<br />
<input type="button" value="선택" onclick="checkForm()">
</form>
</body>
</html>
예시2 라디오박스
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript">
const checkForm = function () {
let chkArr = document.frm.r.length;
for (let i = 0; i < chkArr; i++) {
if (document.frm.r[i].checked) {
console.log(document.frm.r[i].value);
}
}
}
</script>
</head>
<body>
<form name="frm">
<input type="radio" name="r" value="사과">사과<br />
<input type="radio" name="r" value="바나나" checked>바나나<br />
<input type="radio" name="r" value="딸기">딸기<br />
<input type="radio" name="r" value="포도">포도<br />
<br />
<input type="button" value="선택" onclick="checkForm()">
</form>
</body>
</html>
예시3 셀렉트, 옵션
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript">
const checkForm = function () {
console.log(document.frm.sel.options);
console.log(document.frm.sel.options.length);
console.log(document.frm.sel.options[0]);
console.log(document.frm.sel.selectedIndex);
console.log(document.frm.sel.options[document.frm.sel.selectedIndex].text);
}
</script>
</head>
<body>
<form name="frm">
<select name="sel">
<option>사과</option>
<option>바나나</option>
<option selected>딸기</option>
<option>포도</option>
</select>
<input type="button" value="선택" onclick="checkForm()">
</form>
</body>
</html>
예시4 이름 대문자 변경 결과문 보이기
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript">
const capitalizeName = function () {
let name = document.frm.name1.value;
if (name == '') {
alert('이름을 입력하세요');
return false;
}
console.log(name);
let nameArr = name.split(" ");
let newName = "";
for (let name of nameArr) {
newName += name.charAt(0).toUpperCase() + name.slice(1) + " ";
}
document.getElementById('result').innerHTML = newName.trim();
}
</script>
</head>
<body>
<form name="frm">
이름입력 <input type="text" name="name1" placeholder="영문이름 입력">
<input type="button" value="대문자로 변경" onclick="capitalizeName()">
</form>
<hr>
<div id="result"></div>
</body>
</html>
예시5 폼 요소제어로 DOM을 이용한 구구단
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript">
const gugudan = function (dan) {
let html = '';
html += '<table border="1" cellspacing="0" width="150" style="text-align: center;">';
for (let col = 1; col <= 9; col++) {
html += '<tr>';
html += '<td>' + dan + ' X ' + col + ' = ' + (dan * col) + '</td>';
html += '</tr>';
}
html += '</table>';
return html;
};
const createGugudanTable = function (startDan, endDan) {
let html = '<table border="1" cellspacing="0">';
const rowCount = Math.ceil((endDan - startDan + 1) / 3);
for (let row = 0; row < rowCount; row++) {
html += '<tr>';
for (let col = 0; col < 3; col++) {
const currentDan = startDan + (row * 3) + col;
if (currentDan <= endDan) {
html += '<td>' + gugudan(currentDan) + '</td>';
}
}
html += '</tr>';
}
html += '</table>';
return html;
};
const showGugudan = function () {
const startDan = parseInt(document.getElementById('startDan').value);
const endDan = parseInt(document.getElementById('endDan').value);
if (isNaN(startDan) || isNaN(endDan) || startDan > endDan) {
alert('올바른 숫자를 입력해주세요.');
return;
}
const result = document.getElementById('result');
result.innerHTML = createGugudanTable(startDan, endDan);
return false;
};
</script>
</head>
<body>
<form onsubmit="return showGugudan()" style="margin: 8px; border: 1px solid black; padding: 10px;">
시작 단: <input type="number" id="startDan" min="1" max="9" required>
끝 단: <input type="number" id="endDan" min="1" max="9" required>
<button type="submit">구구단 보기</button>
</form>
<hr>
<div id="result"></div>
</body>
</html>