How do I reference the input id's?

I have a list of inputs in a form, each with their own incremented id: Note0, Note1, Note2, Note3, Note4a, Note4b, Note5, … They are of type text or checkbox (as are Note4a and Note4b in the first sentence).

I have a textbox and wish to capture the input values and insert them into the textbox. I’m having trouble referencing the inputs by id.

For input type of text, I used:

if ( === Note3)

For input type of checkbox, I used:

if ( === Note4a && input.checked)

(If the checkbox is not checked, I don’t want to insert its value into the textbox.)

Outside of the function at the top of the js page I declare the form and id’s thusly:

var form = document.getElementById('my-form');
var input;
var Note0, Note1, Note2, Note3, Note4a, Note4b;

The error I am getting is: “TypeError: form.input is undefined”

What changes do I need to make to fix this?

Are you sure that you are actually assigning values to the Note variables at some point and not leaving them all undefined? You haven’t included those assignments in the code you posted.

that tells me that no input element has the name input. It wouldn’t work if more than one element had that name, either.

Looks like the inputs type of text need to be declared like this:

var Note0 = document.getElementById('Note0');

But when I try to grab the input’s value to put it in the textarea setupSummary, the value does not get assigned:

if (Note0.value === !"") {setupSummary.value += "Notes: " + input.value + ", ";}

No error in the console.

But it does grab and show the input type of checkbox correctly with:

if (Note4a.value === !"" && input.checked) {setupSummary.value += "Arm Type: Flat, ";}

well, you check if the input’s value is boolean true, which it never is since it’s a string and not a boolean. (!"" => true)

Hmmm, so I need to check for content, like length:

if (Note0.length > 0) {setupSummary.value += "Notes: " + Note0.value + ", ";}

But that did not work either. Am I on the right path?

OK, this seems to work. Declare input type of text like this:

var Note0 = document.getElementById('Note0').value;

Declare input type of checkbox like this:

var Note4a = document.getElementById('Note4a');

Pass the value of the text input like this:

if (Note0.length >= 0) {setupSummary.value += "Notes: " + Note0 + ", ";} 

Pass the value of the checkbox input like this:

if (Note4a.checked) {setupSummary.value += "Arm Type: Flat, ";}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.