Javascript function not getting all values first time -
i trying let user upload list of items filter search results table by. problem not rows don't have values in filter list removed first time, program want need run multiple times.
the attached code works, want remove 'badworkaround()' function
<div align="center"> <script> function readlist() { var origtext = document.getelementbyid("cusiptextarea").value; var filterlist = origtext.split("\n"); var table = document.getelementbyid("resultstable"); var rows = table.getelementsbytagname("tr"); for(i = 1; < rows.length; i++) #first row has column names start @ i=1 { var found = '0' for(j = 0; j < filterlist.length; j++) { if (rows[i].innerhtml.indexof(filterlist[j]) > 0){ found = '1' break; } } if (found != '1'){rows[i].remove();} } } function badworkaround(){ readlist(); readlist(); readlist(); readlist(); readlist(); readlist(); readlist(); readlist(); readlist(); } </script> <textarea id=textarea>paste in list filter by. 1 per line please.</textarea><br> <button type="button" onclick="return badworkaround();">filter list</button> </div>
nodelists (like getelementsbytagname) “live”, meaning reflect current state of dom at time.
you looping on tr nodes, , removing of them while doing so. lets @ index 1 in loop (second loop iteration), , remove node – following nodes “slide up” 1 position, formerly node index 2 has index 1, , 3 2, etc. since increasing loop counter i
1 each loop, in next iteration access node now 1 @ index 2 (formerly 3) – , node @ position 2 before not processed more.
easiest solution problem: loop on nodes backwards, rows.length - 1
0
(and decrease loop counter variable then, obviously). because if remove node @ end of list, not affect position of nodes before it.
Comments
Post a Comment