Back to Freecodecamp

Add a Submit Button to a Form

curriculum/challenges/english/blocks/basic-html-and-html5/bad87fee1348bd9aedd08830.md

latest2.4 KB
Original Source

--description--

Let's add a submit button to your form. Clicking this button will send the data from your form to the URL you specified with your form's action attribute.

Here's an example submit button:

html
<button type="submit">this button submits the form</button>

--instructions--

Add a button as the last element of your form element with a type of submit, and Submit as its text.

--hints--

Your form should have a button inside it.

js
const form = document.querySelector('form');
const children = form.querySelectorAll(`:scope ${"button"}`); 
assert.isNotEmpty(children); 

Your submit button should have the attribute type set to submit.

js
assert.strictEqual(document.querySelector('button').getAttribute('type'), 'submit');

Your submit button should only have the text Submit.

js
const text = document.querySelector('button').textContent;
assert.match(text,/^\s*submit\s*$/gi);

Your button element should have a closing tag.

js
assert.match(code,/<\/button>/g);
assert.match(code,/<button/g);
assert.strictEqual(code.match(/<\/button>/g).length,code.match(/<button/g).length);

--seed--

--seed-contents--

html
<h2>CatPhotoApp</h2>
<main>
  <p>Click here to view more <a href="#">cat photos</a>.</p>

  <a href="#"></a>

  <p>Things cats love:</p>
  <ul>
    <li>catnip</li>
    <li>laser pointers</li>
    <li>lasagna</li>
  </ul>
  <p>Top 3 things cats hate:</p>
  <ol>
    <li>flea treatment</li>
    <li>thunder</li>
    <li>other cats</li>
  </ol>
  <form action="https://www.freecatphotoapp.com/submit-cat-photo">
    <input type="text" placeholder="cat photo URL">
  </form>
</main>

--solutions--

html
<h2>CatPhotoApp</h2>
<main>
  <p>Click here to view more <a href="#">cat photos</a>.</p>

  <a href="#"></a>

  <p>Things cats love:</p>
  <ul>
    <li>catnip</li>
    <li>laser pointers</li>
    <li>lasagna</li>
  </ul>
  <p>Top 3 things cats hate:</p>
  <ol>
    <li>flea treatment</li>
    <li>thunder</li>
    <li>other cats</li>
  </ol>
  <form action="https://www.freecatphotoapp.com/submit-cat-photo">
    <input type="text" placeholder="cat photo URL">
    <button type="submit">Submit</button>
  </form>
</main>