Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1. Replace `<your_account>` with your Github username in the link
- [DEMO LINK](https://<your_account>.github.io/js_task_fix_form_DOM/)
- [DEMO LINK](https://ybriukhovetska.github.io/js_task_fix_form_DOM/)
2. Follow [this instructions](https://mate-academy.github.io/layout_task-guideline/)
- Run `npm run test` command to test your code;
- Run `npm run test:only -- -n` to run fast test ignoring linter;
Expand Down
42 changes: 41 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
'use strict';

// write code here
const forms = document.querySelectorAll('form');

function capitalize(value) {
if (!value) {
return '';
} else {
const word = value[0].toUpperCase() + value.slice(1);

return word;
}
}

for (const form of forms) {
const inputs = form.querySelectorAll('input');

for (const input of inputs) {
if (
[
'submit',
'button',
'reset',
'hidden',
'image',
'file',
'checkbox',
'radio',
Comment on lines +21 to +28

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exclusion list includes checkbox and radio, which will skip those data inputs. The task requires adding labels/placeholders relying on name for form inputs; only non-data controls (submit/button/reset/hidden/image/file) should be skipped. Remove checkbox and radio from this list so they receive labels/placeholders as well.

].includes(input.type)
) {
continue;
}

const label = document.createElement('label');

label.classList.add('field-label');

label.htmlFor = input.id;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You set label.htmlFor = input.id, but the script never guarantees input.id exists. If an input lacks an id the label won't be associated with the control. Add a helper to generate a safe id (e.g. from input.name by removing/replacing non-alphanumeric characters and ensuring uniqueness by appending a numeric suffix if needed), assign it to input.id, then set label.htmlFor to that id before inserting the label.

label.textContent = capitalize(input.name);
input.placeholder = capitalize(input.name);
input.parentNode.insertBefore(label, input);
}
}
Loading