I'm trying to insert some CSS rules with JavaScript and jQuery for a form.
Unfortunately it doesn't work.
On click of whatever input I want to do some checking on all the inputs with the same "name" and apply or remove classes or CSS rules.
Here's my Html:
<input type="text" class="form-control form-control-lg form-style input1" name="input-class" value="" placeholder="John">
<label class="form-label-input form-label-input-1">Name:</label>
<input type="text" class="form-control form-control-lg form-style input2" name="input-class" value="" placeholder="Doe">
<label class="form-label-input form-label-input-2">Surname:</label>
<input type="text" class="form-control form-control-lg form-style input3" name="input-class" value="" placeholder="Passions">
<label class="form-label-input form-label-input-3">Passions:</label>
Here's my script with some explanations:
//On Click of all the elements with name="input-class"
document.querySelectorAll('input[name="input-class"]').forEach(item => {
item.addEventListener('click', event => {
//Get all Inputs in a Static NodeList
const inputer = document.querySelectorAll('input[name="input-class"]');
//Convert the List into an Array
var btnsArri = Array.from(inputer);
//Loops through all the array on inputs
var i;
for (i = 0; i < btnsArri.length; i++) {
if(btnsArri[i] === document.activeElement){
if(btnsArri[i].value === ""){
console.log(i + ' is empty and selected')
//Recreate the name of the class
j = 1
h = j + i;
var label = 'form-label-input-' + h;
var inputChanger = $(btnsArri[i]).attr('class').split(' ')[3];
console.log(label)
//add CSS Rule
//------------------------THAT'S WHERE I'M STUCK..--------------------
setTimeout(function(){ $(inputChanger).css({"width":"105%"})}, 0);
setTimeout(function(){ $(label).css({"font-size":".6rem"})}, 0);
}
if(btnsArri[i].value !== ""){
console.log(i + ' is full and selected')
}
}
if(btnsArri[i] !== document.activeElement){
if(btnsArri[i].value === ""){
console.log(i + ' is empty')
}
if(btnsArri[i].value !== ""){
console.log(i + ' is full')
}
}
}
})
})
The code is working just fine it's just the part setTimeout(function(){ $(inputChanger).css({"width":"105%"})}, 0);
setTimeout(function(){ $(label).css({"font-size":".6rem"})}, 0); that doesn't work. Worst it's not returning any error So i'm a bit lost.