0

I have a dynamically created fieldset with a form and 6 inputs for name, contact etc. I want get the data from these input fields using something like:

document.getElementsByClassName("input-field")[0].id.value;

but no matter what method of accessing elements I use (ById, ByTagName etc) I always get the error cannot get value of undefined. Why is it undefined when I have 6 input fields with same class name and how can I access the value? Been googling for ages and can't find anything that helps.

EDIT

heres my code

// get body tag
var mainBody = document.getElementsByTagName("body");

// create array of input labels
var inputNameValues = ["first-name", "last-name", "title", "health-num", "email", "telephone"];
var inputLabels = ["First Name", "Last Lame", "Title", "Health Authority Number", "Email", "Telephone (Optional)"];

// create the contact form
var contactPageDiv = document.createElement("div");
contactPageDiv.setAttribute("id", "form-cointainer");
var fieldSet = document.createElement("fieldset");
fieldSet.setAttribute("id", "contact-form")
var form = document.createElement("form");
form.setAttribute("onsubmit", validateForm());
fieldSet.appendChild(form);
// create labels and input fields
for (let i = 0; i < inputLabels.length; i++) {
  var label = document.createElement("label");
  var bold = document.createElement("b");
  var inputText = document.createTextNode(inputLabels[i]);
  bold.appendChild(inputText);
  label.appendChild(bold);
  form.appendChild(document.createElement("br"));
  var input = document.createElement("input");
  input.setAttribute("class", "input-field");
  input.setAttribute("type", "text");
  input.setAttribute("id", inputNameValues[i]);

  // setup placeholder text, focus and required fields
  input.setAttribute("placeholder", inputLabels[i]);
  if (inputNameValues[i] === "first-name") {
    input.setAttribute("autofocus", "true");
  }
  if (inputNameValues[i] !== "telephone") {
    input.setAttribute("required", "true");
  }

  // add tool tip icon
  if (inputNameValues[i] === "health-num") {
    // create the img
    var img = document.createElement("img");
    img.setAttribute("id", "question-mark");
    img.setAttribute("src", "resources/questionmark.jpg");
    img.setAttribute("alt", "question mark symbol");

    // create the tool tip text box
    var textBox = document.createElement("div");
    textBox.setAttribute("id", "tool-tip-text-box");
    var paragraph = document.createElement("p");
    textBox.appendChild(paragraph);
    var text = document.createTextNode("If you do not know your ZHA number, please contact your GP");
    paragraph.appendChild(text);
    label.appendChild(textBox);
    img.onmouseover = function() {
      textBox.style.display = "block";
    }
    img.onmouseout = function() {
      textBox.style.display = "none";
    }
    label.appendChild(img);
  }
  form.appendChild(label);
  form.appendChild(input);
}
// create submit button
var submitBtn = document.createElement("input");
submitBtn.setAttribute("id", "submit-btn");
submitBtn.setAttribute("type", "submit");
submitBtn.setAttribute("value", "Submit!");
form.appendChild(submitBtn);
contactPageDiv.appendChild(fieldSet);

// display the form on the page
mainBody[0].appendChild(contactPageDiv);

function validateForm() {

  console.log(document.getElementsByClassName("input-field")[0].value);

}

7
  • 1
    Please post a minimal reproducible example using the snippet editor Commented Mar 6, 2019 at 8:16
  • 1
    What do you mean by id and [0] will access the FIRST element only Commented Mar 6, 2019 at 8:17
  • 1
    Why use id.....try document.getElementsByClassName("input-field")[0].value; Commented Mar 6, 2019 at 8:18
  • 2
    When do you want the value? Maybe you're trying to get the value before the elements are available. Commented Mar 6, 2019 at 8:19
  • 1
    Remove the () from form.setAttribute("onsubmit", validateForm()); - it is executing immediately Commented Mar 6, 2019 at 8:37

1 Answer 1

1

This is an X/Y problem

Remove the () from form.setAttribute("onsubmit", validateForm());

It is executing immediately before the fields exist

Also use eventListener

form.addEventListener("submit", validateForm);

And I recommend querySelector

function validateForm(e) {
  e.preventDefault(); // remove when you have tested

 // takes the first - use querySelectorAll to get the rest
  console.log(document.querySelector(".input-field").value); 
}

// get body tag
var mainBody = document.getElementsByTagName("body");

// create array of input labels
var inputNameValues = ["first-name", "last-name", "title", "health-num", "email", "telephone"];
var inputLabels = ["First Name", "Last Lame", "Title", "Health Authority Number", "Email", "Telephone (Optional)"];

// create the contact form
var contactPageDiv = document.createElement("div");
contactPageDiv.setAttribute("id", "form-cointainer");
var fieldSet = document.createElement("fieldset");
fieldSet.setAttribute("id", "contact-form")
var form = document.createElement("form");
form.addEventListener("submit", validateForm);
fieldSet.appendChild(form);
// create labels and input fields
for (let i = 0; i < inputLabels.length; i++) {
  var label = document.createElement("label");
  var bold = document.createElement("b");
  var inputText = document.createTextNode(inputLabels[i]);
  bold.appendChild(inputText);
  label.appendChild(bold);
  form.appendChild(document.createElement("br"));
  var input = document.createElement("input");
  input.setAttribute("class", "input-field");
  input.setAttribute("type", "text");
  input.setAttribute("id", inputNameValues[i]);

  // setup placeholder text, focus and required fields
  input.setAttribute("placeholder", inputLabels[i]);
  if (inputNameValues[i] === "first-name") {
    input.setAttribute("autofocus", "true");
  }
  if (inputNameValues[i] !== "telephone") {
    input.setAttribute("required", "true");
  }

  // add tool tip icon
  if (inputNameValues[i] === "health-num") {
    // create the img
    var img = document.createElement("img");
    img.setAttribute("id", "question-mark");
    img.setAttribute("src", "resources/questionmark.jpg");
    img.setAttribute("alt", "question mark symbol");

    // create the tool tip text box
    var textBox = document.createElement("div");
    textBox.setAttribute("id", "tool-tip-text-box");
    var paragraph = document.createElement("p");
    textBox.appendChild(paragraph);
    var text = document.createTextNode("If you do not know your ZHA number, please contact your GP");
    paragraph.appendChild(text);
    label.appendChild(textBox);
    img.onmouseover = function() {
      textBox.style.display = "block";
    }
    img.onmouseout = function() {
      textBox.style.display = "none";
    }
    label.appendChild(img);
  }
  form.appendChild(label);
  form.appendChild(input);
}
// create submit button
var submitBtn = document.createElement("input");
submitBtn.setAttribute("id", "submit-btn");
submitBtn.setAttribute("type", "submit");
submitBtn.setAttribute("value", "Submit!");
form.appendChild(submitBtn);
contactPageDiv.appendChild(fieldSet);

// display the form on the page
mainBody[0].appendChild(contactPageDiv);

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

1 Comment

Thanks yes the error has gone but now nothing prints to the console on submit, not even a simple test message

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.