Skip to content
Open

done #1869

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
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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://github.com/annbisha.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;
- Run `npm run test:only -- -l` to run fast test with additional info in console ignoring linter.
- Run `npm run test` command to test your code;
- Run `npm run test:only -- -n` to run fast test ignoring linter;
- Run `npm run test:only -- -l` to run fast test with additional info in console ignoring linter.

### Task: Auth form fix

Expand All @@ -12,21 +12,24 @@ Look at this form... Looks like something is missing here. Labels? Placeholders?
![Preview](./src/images/preview.png)

Your task is to make script, which fixes problems in this form.
1) Add `<label>` for inputs.
2) Add placeholders for each input.

1. Add `<label>` for inputs.
2. Add placeholders for each input.

Rely on the `name` of the input when writing your script.

You can read about placeholders and labels here:

- [MDN Placeholder attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#attr-placeholder)
- [MDN Label tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label)

##### Steps to do this challenge:
1) Get all `inputs` from `form` tag on the page.
2) For each `input` element create element `label` with `class` `field-label` (it is needed to apply css styles) and `for` attribute where set `id` of current input. Set `textContent` for label rely on `input` name.
3) For each `input` set `placeholder` based on `input` name. Capitalize it.
4) Append `label` elements to the parent container of `input` (do not wrap inputs into the label in this task)
5) Done.

1. Get all `inputs` from `form` tag on the page.
2. For each `input` element create element `label` with `class` `field-label` (it is needed to apply css styles) and `for` attribute where set `id` of current input. Set `textContent` for label rely on `input` name.
3. For each `input` set `placeholder` based on `input` name. Capitalize it.
4. Append `label` elements to the parent container of `input` (do not wrap inputs into the label in this task)
5. Done.

Hints: p.2 and p.4 can be done in one loop

Expand Down
28 changes: 27 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
'use strict';

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

Choose a reason for hiding this comment

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

You call document.querySelectorAll('form') and iterate every form. The task (checklist item #15) requires targeting the form that contains #sign-in-email and #sign-in-password using document.querySelector so the fixes are applied to that specific form. Consider locating the sign-in inputs and using closest('form') (for example: find #sign-in-email and get its closest form) before collecting inputs.


forms.forEach((form) => {
const inputs = form.querySelectorAll('input');

inputs.forEach((input) => {
const inputName = input.name;

if (!input.id) {
input.id = inputName;
}
Comment on lines +11 to +13
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This branch sets input.id = inputName when input.id is missing. If input.name is empty or missing this will assign an empty id (or duplicate ids). Ensure input.name exists and provide a safe fallback id (for example input.id = inputName || 'input-${index}') to avoid invalid/duplicate ids.


const labelText = inputName[0].toUpperCase() + inputName.slice(1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You compute labelText via inputName[0].toUpperCase() + inputName.slice(1) but this will throw if inputName is empty/undefined. Add a guard: only capitalize when inputName is a non-empty string (or fallback to a safe string). Also confirm the label text expected by tests — they expect label text derived from input.name (ensure casing matches test expectations).


// placeholder
input.placeholder = labelText;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You set input.placeholder for every input. Consider skipping non-textual input types (e.g., type='submit', type='button') since placeholders don't apply there; this avoids unnecessary DOM changes and potential confusion.


// label
const label = document.createElement('label');
label.classList.add('field-label');
label.htmlFor = input.id;
label.textContent = labelText;

// вставляем в родителя
input.parentElement.insertBefore(label, input);
});
});
Loading