1

I am trying to create a to-do list using vanilla javascript, when text is entered into the text-input, it will dynamically create an < li >, < h2 > and text. I want to add a radio button dynamically and align it to the end of the li in the center.

I have tried adding display: table-cell; vertical-align center, to the < li > tag, but this didn't work as it made the < li > too small. I also tried display: flex on the < li > tag and justify-content: flex-end; on the radio button, but it would move the radio button into a different position which still was not correct.

I have put the code into a fiddle: https://jsfiddle.net/oub5pm4g/

document.querySelector("#add").addEventListener("click", btnClick);
document.querySelector("#input").addEventListener("keyup", keyUp)

function keyUp(event) {
    if (event.keyCode === 13) {
        event.preventDefault();
        btnClick();
    }
}

function btnClick(event) {
    event.preventDefault()
    var input = document.querySelector("#input");
    var ul = document.querySelector("ul");
    li = document.createElement("li");
    ul.appendChild(li);
    h2 = document.createElement("h2");
    text = document.createTextNode(input.value);
    h2.appendChild(text);
    li.appendChild(h2);
    var radio = document.createElement("input");
    radio.setAttribute("type", "radio");
    li.appendChild(radio);
    document.querySelector("#form").reset();


}
body {
  background: rgb(238, 237, 237);
}

.container {
  background: white;
  max-width: 50%;
  max-height: 80%;
  margin: 0 auto;
  border-radius: 25px;
  padding: 2%;
  box-shadow: 0 5px 10px rgba(154, 160, 185, 0.05),
    0 15px 40px rgba(166, 173, 201, 0.2);
}

h1 {
  font-family: "Roboto", sans-serif;
  font-weight: 900;
  text-align: center;
}

h2 {
  font-weight: 600;
}

h3 {
  font-weight: 00;
}

h4 {
  font-weight: 100;
}

ul {
  list-style: none;
  padding: 0;
  font-family: "Roboto", sans-serif;
  margin-top: 5%;
  color: white;
}

li {
  box-sizing: border-box;
  width: 100%;
  margin-bottom: 5%;
  font-size: 1em;
  background: crimson;
  padding: 1em;
  border-radius: 15px;
}

input[type="text"] {
  box-sizing: border-box;
  width: 85%;
  font-size: 1em;
  padding: 1em;
}

input[type="radio"] {
  float: right;
  align-content: center;
}

button {
  padding: 1em;
  width: 13%;
  font-size: 1em;
  color: white;
  background: crimson;
  margin-left: 1.4%;
}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="main.css" />
    <link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet">

</head>

<body>

    <div class="container">

        <h1>To-Do List</h1>
        <form id="form">

            <input type="text" placeholder="Add new task..." id="input">
            <button id="add" type="submit">Add</button>


        </form>
        <ul>


        </ul>

    </div>

</body>
<script src="main.js"></script>

</html>

Thanks.

justDan
  • 2,302
  • 5
  • 20
  • 29
Oli Knight
  • 49
  • 10

1 Answers1

3

Try flex properties on li tag

li {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

document.querySelector("#add").addEventListener("click", btnClick);
document.querySelector("#input").addEventListener("keyup", keyUp)

function keyUp(event) {
  if (event.keyCode === 13) {
    event.preventDefault();
    btnClick();
  }
}

function btnClick(event) {
  event.preventDefault()
  var input = document.querySelector("#input");
  var ul = document.querySelector("ul");
  li = document.createElement("li");
  ul.appendChild(li);
  h2 = document.createElement("h2");
  text = document.createTextNode(input.value);
  h2.appendChild(text);
  li.appendChild(h2);
  var radio = document.createElement("input");
  radio.setAttribute("type", "radio");
  li.appendChild(radio);
  document.querySelector("#form").reset();


}
body {
  background: rgb(238, 237, 237);
}

.container {
  background: white;
  max-width: 50%;
  max-height: 80%;
  margin: 0 auto;
  border-radius: 25px;
  padding: 2%;
  box-shadow: 0 5px 10px rgba(154, 160, 185, 0.05), 0 15px 40px rgba(166, 173, 201, 0.2);
}

h1 {
  font-family: "Roboto", sans-serif;
  font-weight: 900;
  text-align: center;
}

h2 {
  font-weight: 600;
}

h3 {
  font-weight: 00;
}

h4 {
  font-weight: 100;
}

ul {
  list-style: none;
  padding: 0;
  font-family: "Roboto", sans-serif;
  margin-top: 5%;
  color: white;
}

li {
  box-sizing: border-box;
  width: 100%;
  margin-bottom: 5%;
  font-size: 1em;
  background: crimson;
  padding: 1em;
  border-radius: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

input[type="text"] {
  box-sizing: border-box;
  width: 85%;
  font-size: 1em;
  padding: 1em;
}

input[type="radio"] {
  float: right;
  align-content: center;
}

button {
  padding: 1em;
  width: 13%;
  font-size: 1em;
  color: white;
  background: crimson;
  margin-left: 1.4%;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Page Title</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" media="screen" href="main.css" />
  <link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet">

</head>

<body>

  <div class="container">

    <h1>To-Do List</h1>
    <form id="form">

      <input type="text" placeholder="Add new task..." id="input">
      <button id="add" type="submit">Add</button>


    </form>
    <ul>


    </ul>

  </div>

</body>
<script src="main.js"></script>

</html>
User863
  • 19,346
  • 2
  • 17
  • 41
  • I would recommend you to use `label` instead of `h2` and apply `font-size` on `label`. [Check this](https://stackoverflow.com/questions/1527721/using-label-for-on-radio-buttons) – User863 Jul 02 '19 at 15:37