Dynamic Control Name Access
▼1 up and 0 down, posted by
1 up and 0 down, posted by
Hi All;
I have a form with inputs created dynamically. Their names are generated like
newpackqty1, newpackqty2, newpackqty3, and so on.
I am trying to validate that at least on of these controls, has value this is my Code
for (let i = 1; i <= parseInt(lblCount.value); i++) {
let newqty = 'newpackqty'+i.toString();
if ($("input[name=newqty]").val() == 0) {
noSrcLabels = true;
break;
}
I'd suggest posting on stack overflow. That said.. const checks = Array.from( document.querySelectorAll('input[name^=newpackqty]') ); const noSrcLabels = !checks.find(el => ~~el.value); the first one gets all inputs with a name that starts with newpackqty, and the second checks to find if one has a numeric value that is not 0, and inverts the rsult (!).