Topic: Labels in a second invoice row focus the first row
Amelia Novak
asked a week ago
Our invoice editor clones an item row when the user clicks Add item. The new inputs accept text normally, but clicking their labels puts the cursor into the original row instead.
I kept the IDs from the template so the outline styling would remain consistent. Do those IDs need to change for every row, and does the label need an update too?
Igor Przybysz
staff pro premium priority answered a week ago
Each input ID must be unique across the page. A cloned label whose for value still points to the original ID can focus the original input. Generate a new ID for every cloned field and update its label's for attribute to that same value.
Keep the styling classes on the wrappers and controls; those classes can repeat. Set up the new ID and label association before initializing the inserted wrapper. Check other references in the template, such as aria-describedby, if you clone help or validation messages as well.
const input = newRow.querySelector('input');
const label = newRow.querySelector('label');
input.id = `invoice-description-${nextRowId++}`;
label.htmlFor = input.id;
https://mdbootstrap.com/docs/standard/forms/input-fields/
Amelia Novak commented a week ago
Updating the input ID and label.htmlFor together fixed the focus issue. I also found a repeated help-text ID in the template and made that unique. The new rows now work with both label clicks and keyboard navigation. Thank you.
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- User: Free
- Premium support: No
- Technology: MDB Standard
- MDB Version: MDB5 9.3.0
- Device: Desktop
- Browser: Edge
- OS: Windows 11
- Provided sample code: No
- Provided link: No