5 изменений, все подписаны
let selectedRow = null;
const getS = selector => document.querySelector(selector);
const login = getS('#log');
const pass = getS('#pass');
const mail = getS('#mail');
const form = getS('.form');
const TBODY = getS('.tbody');
const inp = document.querySelectorAll('button');
const btnEdit = getS('.btn-edit');
var id = ''
let loginRegExp = /^\w{4,25}$/;
let passRegExp = /^[a-zA-Z0-9_@#&]{8,15}$/;
let mailRexExp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
function setErrorFor(input) {
if (input.className = 'input') {
input.className = 'input error';
}
}
function setSuccesFor(input) {
if (input.className = 'input') {
input.className = 'input active';
}
}
let arr = [];
function addUser() {
// TBODY.innerHTML = '';
let obj = {
id: Math.floor(Math.random() * 50) + 1,
login: login.value,
password: pass.value,
email: mail.value,
};
arr.push(obj);
render();
}
function render() {
TBODY.innerHTML = '';
arr.forEach((value, index) => {
let newUser = `
<tr data-id='${index}'>
<td>${index+1}</td>
<td>${value.login}</td>
<td>${value.password}</td>
<td>${value.email}</td>
<td> <button onClick = 'onEdit(this)' id ='edit'>edit</button> </td>
<td> <button data-name = 'delete' data-id='${index}' id = 'delete'>delete</button></td>
</tr>`;
TBODY.insertAdjacentHTML('beforeend', newUser);
});
console.dir(TBODY);
}
function onEdit(td) {
let btnEdit = getS('.btn');
console.dir(btnEdit);
inp.forEach(element => {
if (element.className == 'btn') {
element.classList.add('hide')
}
if (element.className == 'btn-edit hide') {
element.classList.remove('hide');
}
});
selectedRow = td.parentElement.parentElement;
id = selectedRow.cells[0].innerHTML
login.value = selectedRow.cells[1].innerHTML;
pass.value = selectedRow.cells[2].innerHTML;
mail.value = selectedRow.cells[3].innerHTML;
td.className = 'input active';
console.dir(selectedRow);
}
function saveEditUser() {
let objNew = {
login: login.value,
password: pass.value,
email: mail.value,
};
arr.splice(id-1, 1, objNew);
render();
getS('.btn-edit').classList.add('hide')
getS('.btn').classList.remove('hide')
}
btnEdit.addEventListener('click', (event) => {
saveEditUser();
form.reset();
});
TBODY.addEventListener('click', function name(event) {
event.stopPropagation();
console.log(event.target.dataset);
if (event.target.dataset.name == "delete") {
arr.splice(event.target.dataset.id, 1);
render();
}
});
login.oninput = function firstNameCheck() {
let testlogin = loginRegExp.test(this.value);
if (testlogin) {
setSuccesFor(login);
} else {
setErrorFor(login);
}
};
pass.oninput = function firstNameCheck() {
let testPass = passRegExp.test(this.value);
if (testPass) {
setSuccesFor(pass);
} else {
setErrorFor(pass);
}
};
mail.oninput = function firstNameCheck() {
let maillogin = mailRexExp.test(this.value);
if (maillogin) {
setSuccesFor(mail);
} else {
setErrorFor(mail);
}
};
form.addEventListener('submit', function name(event) {
event.preventDefault();
if(getS('.btn').classList=='btn') addUser(); // ИЗМЕНЕНИЕ НОМЕР 6
else{saveEditUser()} // ИЗМЕНЕНИНЕ НОМЕР 7
form.reset();
login.className = 'input';
pass.className = 'input';
mail.className = 'input';
});