Scip Posted June 26, 2009 Share Posted June 26, 2009 here is a snippet of the code thats coursing me problems. var i=0; for(i=0;i<cartRows.length; i++){ var resourceName = cartRows[i].getElementsByTagName('td')[0].firstChild; if (resourceName != clonedParent.getElementsByTagName('td')[0].firstChild){ cartTable.appendChild(clonedParent); }else{ alert("You may only pick a resource once."); } it is suppose to go through the rows of a table (cart-table) and compare it to a new row which is going to be entered to see if they are the same before it is entered. The problem is if the comparison is false it inserts the row and then alerts the user. If it false i want to only alert the user. How can i make it do that. I tried switching statements around and tried "==" but doesn't seem to work. thanks in advance. Quote Link to comment Share on other sites More sharing options...
dzelenika Posted June 26, 2009 Share Posted June 26, 2009 your for loop is traversing many rows (I guess) for rows where fields are same you got error, on other fields it inserts. You shouldn't do inserting immediate in for loop. First check if you have searched text, then if not do inserting. Quote Link to comment Share on other sites More sharing options...
Scip Posted June 26, 2009 Author Share Posted June 26, 2009 could you elaborate a little please? I have been wrestling with this problem for a while now lol, tried using "break" but that caused more problems. :s Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 26, 2009 Share Posted June 26, 2009 var newResourceName = clonedParent.getElementsByTagName('td')[0].firstChild; for(var i=0;i<cartRows.length; i++) { var resourceName = cartRows[i].getElementsByTagName('td')[0].firstChild; var duplicate = false; if (resourceName == newResourceName) { duplicate = true; } } if (duplicate) { alert("You may only pick a resource once."); } else { cartTable.appendChild(clonedParent); } Quote Link to comment Share on other sites More sharing options...
Scip Posted June 26, 2009 Author Share Posted June 26, 2009 Thanks i will give it a try. Quote Link to comment Share on other sites More sharing options...
Scip Posted June 26, 2009 Author Share Posted June 26, 2009 Wow thanks so much works like a charm Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 26, 2009 Share Posted June 26, 2009 I just realized I should have added a break statement if a match is found since there would be no reason to check and additional rows after the first match is found. var newResourceName = clonedParent.getElementsByTagName('td')[0].firstChild; for(var i=0;i<cartRows.length; i++) { var resourceName = cartRows[i].getElementsByTagName('td')[0].firstChild; var duplicate = false; if (resourceName == newResourceName) { duplicate = true; break; } } if (duplicate) { alert("You may only pick a resource once."); } else { cartTable.appendChild(clonedParent); } Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 27, 2009 Share Posted June 27, 2009 There's also no reason to define the variable duplicate inside the loop either. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.