Skip to content

Instantly share code, notes, and snippets.

@potikanond
Last active January 21, 2020 08:27
Show Gist options
  • Save potikanond/f233c0c2087a8eba5a1aa398cf2bad26 to your computer and use it in GitHub Desktop.
Save potikanond/f233c0c2087a8eba5a1aa398cf2bad26 to your computer and use it in GitHub Desktop.
JavaScript Simple Form and DOM
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>JS Crash Course</title>
</head>
<body>
<header>
<h1>JS Crash Course</h1>
</header>
<section class="container">
<form id="my-form">
<h1>Add User</h1>
<div class="msg"></div>
<div>
<label for="name">Name:</label>
<input type="text" id="name">
</div>
<div>
<label for="email">Email:</label>
<input type="text" id="email">
</div>
<input class="btn" type="submit" value="Submit">
</form>
<ul id="users"></ul>
<ul class="items">
<li class="item">Item 1</li>
<li class="item">Item 2</li>
<li class="item">Item 3</li>
</ul>
</section>
<!-- put JS at the bottom, allow DOM to finish loading first -->
<script src="main.js">
// embedded JS
</script>
</body>
</html>
// ------ working with the UL in comment -------
// const ul = document.querySelector('.items');
// ul.remove();
// ul.lastElementChild.remove();
// ul.firstElementChild.textContent = 'Hello World';
// ul.children[1].innerText = 'Dome Potikanond';
// ul.lastElementChild.innerHTML = '<h1>The Last Item</h1>';
// --- What to do when clicking the button? ---
// btn.addEventListener('click', (e) => {
// e.preventDefault(); // prevent the default submit input behavior (no refresh)
// // ---- playing ----
// // console.log('click');
// // console.log(e);
// // console.log(e.target);
// // console.log(e.target.value);
// // document.querySelector('#my-form').style.background = '#ccc';
// // document.querySelector('body').classList.add('bg-dark');
// // document.querySelector('.items').lastElementChild.innerHTML = '<h2> Submit button is clicked! </h2>';
// });
const myForm = document.querySelector('#my-form');
const nameInput = document.querySelector('#name');
const emailInput = document.querySelector('#email');
const msg = document.querySelector('.msg');
const userList = document.querySelector('#users');
myForm.addEventListener('submit', onSubmit);
function onSubmit(e) {
e.preventDefault();
//console.log(nameInput.value);
if (nameInput.value === '' || emailInput === '') {
// alert('Please enter all fields.');
msg.classList.add('error');
msg.innerHTML = 'Please enter all fields';
setTimeout( () => msg.remove(), 3000); // remove msg in 3 seconds
// setTimeout( () => {
// msg.classList.toggle('error');
// msg.innerHTML = ''
// }, 3000); // remove msg in 3 seconds
// nameInput.focus();
} else {
// console.log('success');
const li = document.createElement('li');
li.appendChild(document.createTextNode(`${nameInput.value} : ${emailInput.value}`));
userList.appendChild(li);
// clear fields
nameInput.value = '';
emailInput.value = '';
}
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica, sans-serif;
line-height: 1.6;
}
ul {
list-style: none;
}
ul li {
padding: 5px;
background: #f4f4f4;
margin: 5px 0;
}
header {
background: #f4f4f4;
padding: 1rem;
text-align: center;
}
.container {
margin: auto;
width: 500px;
overflow: auto;
padding: 3rem 2rem;
}
#my-form {
padding: 2rem;
background: #f4f4f4;
}
#my-form label {
display: block;
}
#my-form input[type='text'] {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border-radius: 5px;
border: 1px solid #ccc;
}
.btn {
display: block;
width: 100%;
padding: 10px 15px;
border: 0;
background: #333;
color: #fff;
border-radius: 5px;
margin: 5px 0;
}
.btn:hover {
background: #444;
}
.bg-dark {
background: #333;
color: #fff;
}
.error {
background: orangered;
color: #fff;
padding: 5px;
margin: 5px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment