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. Link to comment https://forums.phpfreaks.com/topic/163747-string-comparison-problem/ 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. Link to comment https://forums.phpfreaks.com/topic/163747-string-comparison-problem/#findComment-864102 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 Link to comment https://forums.phpfreaks.com/topic/163747-string-comparison-problem/#findComment-864175 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); } Link to comment https://forums.phpfreaks.com/topic/163747-string-comparison-problem/#findComment-864188 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. Link to comment https://forums.phpfreaks.com/topic/163747-string-comparison-problem/#findComment-864196 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 Link to comment https://forums.phpfreaks.com/topic/163747-string-comparison-problem/#findComment-864205 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); } Link to comment https://forums.phpfreaks.com/topic/163747-string-comparison-problem/#findComment-864259 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. Link to comment https://forums.phpfreaks.com/topic/163747-string-comparison-problem/#findComment-864515 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.