0

Alright, so I am stuck. So the break down of the project is when you enter your age, and select an option from the drop down list when you click add, a list is created and that input is plugged into the list. I have to also show that list somewhere on the page. I hope I explained that correctly.

Anyways, I've been trying everything from trying to create a new div, to creating an unordered list programmatically, and trying to display it within the body under the last div, but my code below doesn't execute when I hit the add button. By the way I CAN NOT edit the HTML in anyway and I CAN NOT use JQuery. I have to use pure Javascript. Any tips or help would be great!

var form = document.getElementsByTagName("form")[0];
form.method = "POST";
form.action = "form-handler";

var add = document.getElementsByClassName('add');
add.onlick = 'addToList()';


var age = document.getElementsByName("age")[0];
age.type = "number";
age.required = true;
age.min = "0";
age.max = "120";

var dropDown = document.getElementsByName("rel")[0];
dropDown.type = "option";
dropDown.required = true;

var newDiv = document.createElement("div");
newDiv.setAttribute("id", "houseMem");
document.body.appendChild(newDiv);

//var title = document.createElement("h2");
//title = "Member List";
//newDiv.appendChild(title);*

var ul = document.createElement("ul");
ul.setAttribute("id", "memList");
newDiv.appendChild(ul);


function addToList() {
  var li = document.createElement("li");
  //li.setAttribute('id', age.value + dropDown.value);
  li.appendChild(document.createTextNode(age.value + ' ' + dropDown.value));
  ul.appendChild(li);

  return false;

}
<h1>Household List</h1>
<div class="builder">
  <o class="household"></o>
  <form>
    <div>
      <label>Age
          <input type="text" name="age">
        </label>
    </div>
    <div>
      <label>Relationship
          <select name="rel">
            <option value="">---</option>
            <option value="self">Self</option>
            <option value="spouse">Spouse</option>
            <option value="child">Child</option>
            <option value="parent">Parent</option>
            <option value="grandparent">Grandparent</option>
            <option value="other">Other</option>
          </select>
        </label>
    </div>
    <div>
      <label>Smoker?
          <input type="checkbox" name="smoker">
        </label>
    </div>
    <div>
      <button class="add">add</button>
    </div>
    <div>
      <button type="submit" class="GapViewItemselected">submit</button>
    </div>
  </form>
</div>
<pre class="debug"></pre>

1
  • You can’t assign a string as an event handler. Use add.onclick = addToList;, or better, add.addEventListener("click", addToList); Commented Dec 31, 2017 at 4:30

3 Answers 3

1

There was some syntactical errors in the code, especially the button click event handler. Here is the correct code.

var form = document.getElementsByTagName("form")[0];
form.method = "POST";
form.action = "form-handler";

var add = document.getElementsByClassName('add');
add[0].onclick = addToList;


var age = document.getElementsByName("age")[0];
age.type = "number";
age.required = true;
age.min = "0";
age.max = "120";

var dropDown = document.getElementsByName("rel")[0];
dropDown.type = "option";
dropDown.required = true;

var newDiv = document.createElement("div");
newDiv.setAttribute("id", "houseMem");
document.body.appendChild(newDiv);

//var title = document.createElement("h2");
//title = "Member List";
//newDiv.appendChild(title);*

var ul = document.createElement("ul");
ul.setAttribute("id", "memList");
newDiv.appendChild(ul);


function addToList() {
  var li = document.createElement("li");
  //li.setAttribute('id', age.value + dropDown.value);
  li.appendChild(document.createTextNode(age.value + ' ' + dropDown.value));
  ul.appendChild(li);

  return false;

}
<h1>Household List</h1>
<div class="builder">
  <ol class="household"></o>
  <form>
    <div>
      <label>Age
          <input type="text" name="age">
        </label>
    </div>
    <div>
      <label>Relationship
          <select name="rel">
            <option value="">---</option>
            <option value="self">Self</option>
            <option value="spouse">Spouse</option>
            <option value="child">Child</option>
            <option value="parent">Parent</option>
            <option value="grandparent">Grandparent</option>
            <option value="other">Other</option>
          </select>
        </label>
    </div>
    <div>
      <label>Smoker?
          <input type="checkbox" name="smoker">
        </label>
    </div>
    <div>
      <button class="add">add</button>
    </div>
    <div>
      <button type="submit" class="GapViewItemselected">submit</button>
    </div>
  </form>
</div>
<pre class="debug"></pre>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This helped a ton.
1

There are numerous issues in your code

  1. All the for attributes and input attributes can be defined the html instead of using js

  2. button type default is submit ,so add button type="button" in <button class="add">add</button>

  3. add.onlick = 'addToList()'; is wrong ;the function is not a string , replace it with document.getElementsByClassName('add')[0].onclick = addToList;

var age = document.getElementsByName("age")[0];
var dropDown = document.getElementsByName("rel")[0];

var newDiv = document.createElement("div");
newDiv.setAttribute("id", "houseMem");
document.body.appendChild(newDiv);


var ul = document.createElement("ul");
ul.setAttribute("id", "memList");
newDiv.appendChild(ul);

document.getElementsByClassName('add')[0].onclick = addToList;

function addToList() {
  var li = document.createElement("li");
  li.appendChild(document.createTextNode(age.value + ' ' + dropDown.value));
  ul.appendChild(li);
  return false;

}
<body>
  <h1>Household List</h1>
  <div class="builder">
    <o class="household"></o>
    <form method="POST" action="form-handler">
      <div>
        <label>Age
          <input type="number" name="age" min="0" max="120">
        </label>
      </div>
      <div>
        <label>Relationship
          <select name="rel" required>
            <option value="">---</option>
            <option value="self">Self</option>
            <option value="spouse">Spouse</option>
            <option value="child">Child</option>
            <option value="parent">Parent</option>
            <option value="grandparent">Grandparent</option>
            <option value="other">Other</option>
          </select>
        </label>
      </div>
      <div>
        <label>Smoker?
          <input type="checkbox" name="smoker">
        </label>
      </div>
      <div>
        <button class="add" type="button">add</button>
      </div>
      <div>
        <button type="submit" class="GapViewItemselected">submit</button>
      </div>
    </form>
  </div>


</body>

1 Comment

Thank you, this helps, but I am unable to make changes to the HTML per project specifications. Could I add a type to the add button such as: add.type = ('button'); ?
0

There is a problem with your HTML you have an o tag instead of an order list tag. This may fix the code. Also when you click on the add button the form is posted so I would remove the post until the end.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.