Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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://Ruslan-oss-max0.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
18 changes: 17 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
'use strict';

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

Choose a reason for hiding this comment

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

The task requires getting all input elements from the form tag. Your current selector 'input' will find all inputs on the entire page, which might include some that are not part of the form. To be more precise, you should scope your query to the form element, for example: document.querySelectorAll('form input').


inputsList.forEach((input) => {
const inputLabel = document.createElement('label');

inputLabel.className = 'field-label';
inputLabel.textContent = input.name;
inputLabel.htmlFor = input.id;

inputLabel.textContent = input.name;
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 line is a duplicate of line 9. You can safely remove it to make the code cleaner.


input.parentNode.insertBefore(inputLabel, input);

const firstletter = input.name[0].toUpperCase();

input.placeholder = `${firstletter}${input.name.slice(1)}`;
});
Loading