Javascript verifying input on multiple input form

cspdmg
cspdmg used Ask the Experts™
on
I have an form based on code developed by Cody Lindley that allows the user to create as many entries as needed using a duplicate input form. On the checkbox, I have an onClick event called chkTag() which, if the box is checked, the corresponding tag# field defaults to 'UNKNOWN' and is disabled. This works great for the first instance which is defined. It does not work for the dynamically created instances. It seems that it only sees the first entry. How do you access each instance whether it is a newly created one or a previous one? (i.e. I may have four but i want to change the second one.) Example of code is attached.
index-question.html
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sr. System Analyst
Commented:
you should give them different id's

name='Unknown2' alt='Unknown2' id='Unknown2'

you are using same name & id each time and function is finding the first time... so whenever you create a new one you should create a new one as

<input name='tag_1' alt='tag_1' id='tag_1'
name='Unknown2_1' alt='Unknown2_1' id='Unknown2_1' type='checkbox' onclick='chkTag(this,1)'
name='Unknown2_2' alt='Unknown2_2' id='Unknown2_2' type='checkbox' onclick='chkTag(this,2)'
name='Unknown2_3' alt='Unknown2_3' id='Unknown2_3' type='checkbox' onclick='chkTag(this,3)'

and some changes to function
function chkTag(n) {
  var tag = document.getElementById("tag_"+n);
  if (this.checked) {
    tag.value = "UNKNOWN";
    tag.disabled = true;
  }else{
    tag.value = "";
    tag.disabled = false;
  }
} 

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial