javascript - Looping over array and comparing to regex -
so, i'll admit being bit of js noob, far can tell, should working , not.
background:
i have form 3 list boxes. list boxes named app1, db1, , db2. i'm using javascript allow user add additional list boxes, increasing name tag each additional select box.
when add additional app named boxes, value increments each additional field. if try add addtional db named selects, fails recognize 2nd tag on first loop through array. causes me end 2 elements named db2. on each subsequent tag, recognized , incremented.
here html db1 tag:
<select name="db1"> *options* </select> and db2:
<select name="db2"> *options* </select> the tags identical. here function using figure out next number in sequence (note: tag either app or db, tags array of select tag names in dom, if inspect tags, gives me ['app1', 'db1', 'db2', '']):
function return_select_name(tag, tags) { matches = new array(); var re = new regexp(tag + "\\d+", "g"); (var = 0; < tags.length; i++) { var found = re.exec(tags[i]); if (found != null) { matches.push(found[0]); } } matches = matches.sort(); index = parseint(/\d+/.exec(matches.last())) + 1; index = tag + index; return index; } if add app tag, return 'app2'. if search db tag, return 'db2' on first time through, db3 on 2nd, etc, etc.
so basically, i'm sure i'm doing wrong here.
i'd handle keeping counter db , counter app use generate names.
var appcounter = 1;//set manually or initialize 0 , var dbcounter = 2;//use create function add elements on pageload then, when go create next tag, increment counter , use suffix name:
var newappelement = document.createelement('select'); newappelement.name = 'app' + (++appcounter); .. // --or db element-- var newdbelement = document.createelement('select'); newdbelement.name = 'db' + (++dbcounter ); ..
Comments
Post a Comment