I have problems, I wrote code in Jquery, the worker asks not to use it. This function should check the inputs for fullness
JavaScript
x
14
14
1
$('document').ready(function() {
2
$('#btn').on('click', function() {
3
$('.registration__labels .rfield').each(function() {
4
if ($(this).val() == '') {
5
console.log(2);
6
$('input[required]').addClass('error');
7
} else {
8
console.log(1);
9
$('input[required]').removeClass('error');
10
}
11
});
12
});
13
});
14
here is the Jquery code, it works
JavaScript
1
14
14
1
document.addEventListener('DOMContentLoaded',function validation (){
2
if(btn.addEventListener("click", function() {
3
('.registration__labels .rfield').forEach(() => {
4
if (document.getElementsByClassName('input[type=text]').value == '') {
5
console.log(2);
6
(".input[required]").classList.add('error');
7
}
8
else {
9
console.log(1);
10
(".input[required]").classList.remove('error');
11
}
12
}
13
)}))[validation]});
14
and this is me trying to translate it into javascript and it doesn’t work. Help me pls
Advertisement
Answer
a way to do that…
JavaScript
1
14
14
1
window.addEventListener('DOMContentLoaded', () =>
2
{
3
const allRegLabs = document.querySelectorAll('.registration__labels .rfield')
4
5
document.querySelector('#btn').onclick = () =>
6
{
7
allRegLabs.forEach( el =>
8
{
9
let err = el.classList.toggle('error', (el.value === ''))
10
console.log( (err ? '2':'1' ))
11
})
12
}
13
})
14