Back to Freecodecamp

Step 9

curriculum/challenges/english/blocks/learn-form-validation-by-building-a-calorie-counter/63b60aaaa65f8922bfce6b7e.md

latest5.1 KB
Original Source

--description--

In your span element, create a label element for an entry-dropdown and give it the text Add food or exercise:. Then create a select element with the id set to entry-dropdown and a name set to options. Below that, add a button element with the id set to add-entry and the text Add Entry.

Give your button element a type attribute set to button to prevent automatic form submission.

--hints--

You should add a label element to your span element.

js
assert.exists(document.querySelector('.controls > span > label'));

Your new label element should have a for attribute set to entry-dropdown.

js
assert.equal(document.querySelector('.controls > span > label')?.getAttribute('for'), 'entry-dropdown');

Your new label element should have the text Add food or exercise:.

js
assert.equal(document.querySelector('.controls > span > label')?.innerText, 'Add food or exercise:');

You should add a select element to your span element.

js
assert.exists(document.querySelector('.controls > span > select'));

Your select element should come after your label element.

js
assert(document.querySelector('.controls > span > select')?.previousElementSibling?.tagName === 'LABEL');

Your new select element should have an id attribute set to entry-dropdown.

js
assert.equal(document.querySelector('.controls > span > select')?.getAttribute('id'), 'entry-dropdown');

Your new select element should have a name attribute set to options.

js
assert.equal(document.querySelector('.controls > span > select')?.getAttribute('name'), 'options');

Your select element should be empty and have a closing </select> tag.

js
assert.equal(document.querySelector('.controls > span > select')?.innerHTML?.trim(), '');

You should add a button element to your span element.

js
assert.exists(document.querySelector('.controls > span > button'));

Your button element should come after your select element.

js
assert(document.querySelector('.controls > span > button')?.previousElementSibling?.tagName === 'SELECT');

Your new button element should have an id attribute set to add-entry.

js
assert.equal(document.querySelector('.controls > span > button')?.getAttribute('id'), 'add-entry');

Your new button element should have the text Add Entry.

js
assert.equal(document.querySelector('.controls > span > button')?.innerText, 'Add Entry');

--seed--

--seed-contents--

html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="styles.css" />
    <title>Calorie Counter</title>
  </head>
  <body>
    <main>
      <h1>Calorie Counter</h1>
      <div class="container">
        <form id="calorie-counter">
          <label for="budget">Budget</label>
          <input
            type="number"
            min="0"
            id="budget"
            placeholder="Daily calorie budget"
            required
          />
          <fieldset id="breakfast">
            <legend>Breakfast</legend>
            <div class="input-container"></div>
          </fieldset>
          <fieldset id="lunch">
            <legend>Lunch</legend>
            <div class="input-container"></div>
          </fieldset>
          <fieldset id="dinner">
            <legend>Dinner</legend>
            <div class="input-container"></div>
          </fieldset>
          <fieldset id="snacks">
            <legend>Snacks</legend>
            <div class="input-container"></div>
          </fieldset>
          <fieldset id="exercise">
            <legend>Exercise</legend>
            <div class="input-container"></div>
          </fieldset>
--fcc-editable-region--
          <div class="controls">
            <span>

            </span>
          </div>
--fcc-editable-region--
        </form>
      </div>
    </main>
  </body>
</html>
css
:root {
  --light-grey: #f5f6f7;
  --dark-blue: #0a0a23;
  --fcc-blue: #1b1b32;
  --light-yellow: #fecc4c;
  --dark-yellow: #feac32;
  --light-pink: #ffadad;
  --dark-red: #850000;
  --light-green: #acd157;
}

body {
  font-family: "Lato", Helvetica, Arial, sans-serif;
  font-size: 18px;
  background-color: var(--fcc-blue);
  color: var(--light-grey);
}

h1 {
  text-align: center;
}

.container {
  width: 90%;
  max-width: 680px;
}

h1,
.container,
.output {
  margin: 20px auto;
}

label,
legend {
  font-weight: bold;
}

.input-container {
  display: flex;
  flex-direction: column;
}

button {
  cursor: pointer;
  text-decoration: none;
  background-color: var(--light-yellow);
  border: 2px solid var(--dark-yellow);
}

button,
input,
select {
  min-height: 24px;
  color: var(--dark-blue);
}

fieldset,
label,
button,
input,
select {
  margin-bottom: 10px;
}

.output {
  border: 2px solid var(--light-grey);
  padding: 10px;
  text-align: center;
}

.hide {
  display: none;
}

.output span {
  font-weight: bold;
  font-size: 1.2em;
}

.surplus {
  color: var(--light-pink);
}

.deficit {
  color: var(--light-green);
}
js